简体   繁体   English

scrollTop似乎不正确

[英]scrollTop doesn't seem to be correct

I want to write custom scroll to sections functionality. 我想编写自定义滚动到节功能。

When I load the page and scroll down, it should scroll down the height of the window, which is fine. 当我加载页面并向下滚动时,它应该向下滚动窗口的高度,这很好。

Then, if scrolling down again, I want to check if the page has scrolled more than window height (which should be, if scrolling down), but it seems to be stuck at where it's at after the first scroll. 然后,如果再次向下滚动,我想检查页面滚动的幅度是否超过窗口高度(如果向下滚动,则应该是),但是它似乎停留在第一次滚动之后的位置。

So I can get from red to yellow, but not from yellow to green. 所以我可以从红色变成黄色,但不能从黄色变成绿色。

var winHeight = window.innerHeight;
$('div').each(function(){
        $(this).css('height', winHeight);
});

function scrollSections() {

        var windowHeight = window.innerHeight;
        var scrollTop = $(window).scrollTop();
        var pos = windowHeight;

        if (scrollTop > windowHeight) {
            pos = windowHeight * 2;
        }

        $('body, html').animate({scrollTop: pos}, 1000, function() {
        });
}

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

        scrollSections();

    });

Fiddle: https://jsfiddle.net/zts70yc1/ 小提琴: https : //jsfiddle.net/zts70yc1/

When you call $('body, html').animate({scrollTop: pos}, ...); 当您调用$('body, html').animate({scrollTop: pos}, ...); , you are triggering scroll events, which will run your handler again, which will start an animation again, based on the current (not final) scroll position, and so on. ,您将触发滚动事件,滚动事件将再次运行您的处理程序,并根据当前(而非最终)滚动位置再次启动动画,依此类推。 This multiplies the number of scrolls that happen: this is not what you want. 这会使发生的滚动数量成倍增加:这不是您想要的。

Instead, before doing anything else in your event handler, check whether an animated scrolling is ongoing, and decide what to do then. 相反,在事件处理程序中执行其他任何操作之前,请检查动画滚动是否正在进行,然后决定要执行的操作。 For instance, you could just exit the handler in that case, so the animation can continue without interference: 例如,在这种情况下,您可以退出处理程序,因此动画可以继续进行而不会受到干扰:

if ($('body, html').is(':animated')) return; // exit when animation ongoing

Also, the formula for setting the target position could be improved, by comparing first the scroll position to the previous scroll position. 另外,通过首先将滚动位置与先前的滚动位置进行比较,可以改善用于设置目标位置的公式。 If higher, then scroll one "page" down, else one "page" up. 如果更高,则向下滚动一个“页面”,否则向上滚动一个“页面”。 For that, you should keep pos as a global variable, and do as follows: 为此,应将pos保留为全局变量,并执行以下操作:

var pos = $(window).scrollTop();
function scrollSections() {
        if ($('body, html').is(':animated')) return;
        var windowHeight = window.innerHeight;
        var scrollTop = $(window).scrollTop();
        if (pos == scrollTop) return; // no change
        // depending on scroll direction, go page up, down
        pos += pos < scrollTop ? windowHeight : -windowHeight;
        // round to nearest page boundary
        pos = Math.round(pos / windowHeight) * windowHeight;
        $('body, html').animate({scrollTop: pos}, 1000, function() {
        });
}

These changes have been applied in this fiddle . 这些更改已应用到这个小提琴中

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

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