簡體   English   中英

jQuery-通過鼠標滾輪將視差頁面平滑滾動到某些錨點

[英]jQuery- smooth scrolling a parallax page to certain anchors by mousewheel

我正在使用Stellar.js處理視差頁面,我像這樣將一些視差窗格彼此堆疊在一起:

當用戶向下滾動到第二個窗格時,它必須自動滾動才能到達頁面頂部

當用戶向下滾動頁面並到達該窗格時,我希望每個窗格都能平滑滾動到頂部。 我的意思是我希望滾動器足夠聰明,以使每個窗格與屏幕頂部對齊。 我試過了但是沒用:

h = $(window).height();
t = $('#parallaxtop').offset().top + $('#parallaxtop').height();
if(t > h) {
    $(window).scrollTop(t - h);
}

這是JSFiddle: http//jsfiddle.net/LDaUw/

我發現這個工作:

var pageH = $(window).innerHeight(); //grab window height
    $(window).scroll(function() {
        clearTimeout($.data(this, 'scrollTimer'));
        $.data(this, 'scrollTimer', setTimeout(function() {
            var eTop = $('#parallaxtop').offset().top; //get the offset top of the element
            var myOffset = eTop - $(window).scrollTop(); //determine the offset from window
            if (myOffset > -100 && myOffset < pageH/2) { //if the offset is less than the half of page scroll to the panel
                    $('#parallaxtop').ScrollTo({ //ScrollTo JQuery plugin
                    });
            }
        }, 250)); //this will be calculated 250ms after finishing every scroll
    });

我已經使用ScrollTo JQuery插件進行平滑滾動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM