简体   繁体   English

jQuery 100%滚动到第一部分

[英]Jquery 100% scroll to first section

I'm trying to scroll 100% of the page to the first section and than have the normal scroll through others sections and vice versa. 我试图将页面的100%滚动到第一部分,然后比其他部分正常滚动,反之亦然。

I've tried different approach with no results except a loop.. check this fiddle. 我试着用不同的循环没有结果不同的方法。检查琴。

This is the structure of the page and every elements has 100% height: 这是页面的结构,每个元素都有100%的高度:

<header>Header content...</header>
<section>Section content...</section>
<section>Section content...</section>
<section>Section content...</section>

Javascript 使用Javascript

$(function () {
    var lastScrollTop = $(window).scrollTop(),
        delta = 5,
        eleH = $('header').outerHeight();
    $(window).scroll(function () {
        var nowScrollTop = $(this).scrollTop();
        if (Math.abs(lastScrollTop - nowScrollTop) >= delta) {
            if (nowScrollTop <= eleH && nowScrollTop > lastScrollTop) {
                $('html,body').animate({
                    scrollTop: $('body section:first-of-type').offset().top
                }, 600);
                console.log('Scroll down');
            } else if (nowScrollTop <= eleH && nowScrollTop < lastScrollTop) {
                $('html,body').animate({
                    scrollTop: 0
                }, 600);
                console.log('Scroll up');
            }
            lastScrollTop = nowScrollTop;
        }
    });
});

CSS CSS

html, body {
    height:100%
}
header, section {
    display:block;
    width:100%;
    height:100%;
}
header {
    background-color:gray;
}

As plug-in I've already used Alton (demo link in the fiddle) but it doesn't work quite well if stressed or with the inertia scroll of osX. 作为插件,我已经使用过Alton(小提琴中的演示链接),但是如果承受压力或使用osX的惯性滚动,则效果不佳。

To get this to work I added a variable isScrolling which indicates whether the javascript is busy doing the scrolling. 为了isScrolling起作用,我添加了一个变量isScrolling ,该变量指示javascript是否正在忙于滚动。 When the scroll function is complete I set that variable to false and also recalculate the lastScrollTop 当滚动功能完成时,我将该变量设置为false并重新计算lastScrollTop

JSFiddle 的jsfiddle

$(function () {
    var lastScrollTop = $(window).scrollTop(),
        delta = 5,
        eleH = $('header').outerHeight(),
        isScolling = false ;
    $(window).scroll(function () {
        if(isScolling)
            return;

        var nowScrollTop = $(this).scrollTop();
        if (Math.abs(lastScrollTop - nowScrollTop) >= delta) {
            if (nowScrollTop <= eleH && nowScrollTop >= lastScrollTop) {
                isScolling = true;
                $('html,body').animate({
                    scrollTop: $('body section:first-of-type').offset().top
                }, 600, function() {
                    isScolling = false;
                    lastScrollTop = $(window).scrollTop();
                });
                console.log('Scroll down');
            } else if (nowScrollTop <= eleH && nowScrollTop < lastScrollTop) {
                isScolling = true;
                $('html,body').animate({
                    scrollTop: 0
                }, 600, function() {
                    isScolling = false;
                    lastScrollTop = $(window).scrollTop();
                });
                console.log('Scroll up');
            }
            lastScrollTop = nowScrollTop;
        }
    });
});

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

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