简体   繁体   English

进入视口时,仅自动滚动嵌入式窗口一次。 无法向上滚动

[英]Auto-scroll embedded window only once when entering viewport. Can't scroll back up

I have an image embedded in a container with a background image to give the effect of scrolling within the page. 我将图像嵌入到具有背景图像的容器中,以提供在页面内滚动的效果。 Initially, I had the scrolling effect take place on page load, with this simple bit of script which worked perfectly. 最初,滚动效果是在页面加载时发生的,这个简单的脚本可以很好地工作。

 $(window).on("load", function () {
    $(".embedded_scroller_image").animate({ scrollTop: $('.embedded_scroller_image')[0].scrollHeight}, 2500, "easeInOutCubic");
}); // end on load 

However, the element is too far down the page now and I want that animation to fire when the element enters 80% of the viewport. 但是,该元素现在离页面太远了,我希望当该元素进入视口的80%时触发该动画。 That part is also working fine with this code here (I'm using a scroll limiter to improve browser performance) 该部分在此代码中也可以正常工作(我正在使用滚动限制器来提高浏览器性能)

 // limit scroll call for performance
     var scrollHandling = {
      allow: true,
      reallow: function() {
        scrollHandling.allow = true;
      },
    delay: 500 //(milliseconds) adjust to the highest acceptable value
  };

$(window).on('scroll', function() {
  var flag = true;
    if(scrollHandling.allow) { // call scroll limit
        var inViewport = $(window).height()*0.8; // get 80% of viewport
        $('.embedded_scroller_image').each(function() { // check each embedded scroller
            var distance = $(this).offset().top - inViewport; // check when it reaches offset
            if ($(window).scrollTop() >= distance && flag === true ) {
              $(this).animate({ scrollTop: $(this)[0].scrollHeight}, 2500, "easeInOutCubic"); //animate embedded scroller
              flag = false;
            } 
          });
      } // end scroll limit
  }); // end window scroll function

The problem is this: I want the autoscroll to happen once and then stop. 问题是这样的:我希望自动滚动发生一次然后停止。 Right now, it works on entering viewport, but if I then try to manually scroll the image, it keeps pushing back down or stutters. 现在,它可以进入视口,但是如果我尝试手动滚动图像,它会继续向后推或卡住。 You can't get the element to scroll normally. 您无法使元素正常滚动。 I attempted to use the flag in the code to stop the animation, but couldn't get that to successfully work. 我试图在代码中使用该标志来停止动画,但无法使其成功运行。

How can I have this animation fire when the element is 80% in the viewport, but then completely stop after one time? 当元素在视口中占80%,但是在一段时间后完全停止时,如何触发此动画?

Here is a codepen I mocked up as well http://codepen.io/jphogan/pen/PPQwZL?editors=001 If you scroll down, you will see the image element autoscroll when it enters the viewport, but if you try to then scroll that image up in its container, it won't work. 这也是我也模拟的一个Codepen http://codepen.io/jphogan/pen/PPQwZL?editors=001如果向下滚动,则进入视口时会看到图像元素自动滚动,但是如果尝试将该图像向上滚动到其容器中,将无法正常工作。

Thanks! 谢谢!

I have tweaked your script a bit: 我对您的脚本进行了一些调整:

// limit scroll call for performance
var scrollHandling = {
    allow: true,
    reallow: function() { scrollHandling.allow = true; },
    delay: 500 //(milliseconds) adjust to the highest acceptable value
};

$(window).on('scroll', function() {

if(scrollHandling.allow) { // call scroll limit
    var inViewport = $(window).height()*0.8; // get 80% of viewport

    $('.embedded_scroller_image').each(function() { // check each embedded scroller
        var distance = $(this).offset().top - inViewport; // check when it reaches offset

        if ($(window).scrollTop() >= distance ) {
            $(this).animate({ scrollTop: $(this)[0].scrollHeight}, 2500, "easeInOutCubic"); //animate embedded scroller
            scrollHandling.allow = false;
        } 

    });

} // end scroll limit

}); // end window scroll function

I have kicked out your flag and simply made use of scrollHandling.allow declared already. 我已经踢了你的旗子,只是简单地使用了已经声明的scrollHandling.allow。

Try if it works for you :) 尝试一下是否适合您:)

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM