문제

At my page, i have a few booleans in my script, to check if the first screen is activated, and the same thing for the second screen. Screen 1 fills up the whole screen.

It looks something like this:

var headerLoaded = true,
    contentLoaded = false;

When i scroll, i want the page to scroll automaticly to the 'content' area of my webpage. And i had this code for it:

$(window).scroll(function() {
    if (($(window).scrollTop() > 0) && (!contentLoaded && headerLoaded)) {
        $('html, body').animate({
            scrollTop: $('#content').offset().top
        }, 500, function() {
            contentLoaded = true;
            headerLoaded = false;
        });
    }
});

It checks if im not at the top of the page, and for the 2 booleans.

The function works great, but it's still calling the

        $('html, body').animate({
            scrollTop: $('#content').offset().top
        }, 500, function() {
            contentLoaded = true;
            headerLoaded = false;
        });

part when i'm scrolled down to the 'content' div.

So everytime i scroll when im at the 'content' div, its scrolling back to the top of that.

Btw, both of the div's are absolute.

도움이 되었습니까?

해결책

So, the animate is calling your original $(window).scroll(...) over and over.

Try telling your main scroll listener not to start animating if its already animating.

var animating = false;
$(window).scroll(function() {
    if (!animating && $(window).scrollTop() > 0 && !contentLoaded && headerLoaded) {
        animating = true;
        $('html, body').animate({
            scrollTop: $('#content').offset().top
        }, 500, function() {
            animating = false;
            contentLoaded = true;
            headerLoaded = false;
        });
    }
});

wait.. i just read it.. You're just locking the screen at the content... why animate at all?

The answer I was giving was saying the animating the scroll will also trigger the main scroll, resulting in an endless loop... but what are you trying to do in the first place?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top