简体   繁体   中英

Sticky/snap scroll doesn't scroll back up

I've got a script that will allow for snap scrolling when you move down but I can't get it to allow the user to scroll back upwards.

var items = $(".item");
var animating = false;

$(window).scroll(function() {
    clearTimeout($.data(this, 'scrollTimer'));
    if (!animating) {
        $.data(this, 'scrollTimer', setTimeout(function() {
            items.each(function(key, value) {
                if ($(value).offset().top > $(window).scrollTop()) {
                    animating = true;
                    $('body').animate( { scrollTop: $(value).offset().top + 'px' }, 1000);
                    setTimeout(function() { animating = false; }, 500);
                    return false;
                }
            });
        }, 50));
    }
});

http://jsfiddle.net/kZY9R/77/

You have to do

var body = $("html, body");

and

$(body).stop().animate( { scrollTop: $(value).offset().top)}, 1000,'swing');

Chrome reading body and srolling, Firefox need html to do it

Check Working Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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