将视口锁定到div

时间:2016-07-18 11:41:22

标签: javascript html css3

最近我一直在开发一个网站,我想知道是否有办法将视口锁定到一个div,这样当你向下滚动时它会自动滚动到下一部分而不是你滚过它然后锁定直到你再次滚动?

所以我考虑了人们的建议,现在我把它作为我的jQuery:

$(function(){
    var lastScroll = 0;
    var posNum = 1;
    var currentDiv = "#view";
    $(window).scroll(function(event){
        var st = $(this).scrollTop();
        if (st > lastScroll){
            posNum = posNum + 1;
            if (posNum > 6) {
                posNum = 6;
            }
            currentDiv = currentDiv + posNum;
            $('html, body').animate({
                scrollTop: $(currentDiv).offset().top
            }, 2000);
        }
        else {
            posNum = posNum - 1;
            if (posNum < 1) {
                posNum = 1;
            }
            currentDiv = currentDiv + posNum;
            $('html, body').animate({
                scrollTop: $(currentDiv).offset().top
            }, 2000);
        }
        lastScroll = st;
    });
});

但现在我收到一条错误消息,说无法读取属性&#39; top&#39;属性未定义,它仅适用于首次使用网站(刷新时)。

0 个答案:

没有答案