問題:我想完成以下動畫:當頁面在移動設備中加載時,我想顯示ID爲「sub-header」的div,但只要用戶滾動超過50px的頁面我想要隱藏子標題。頁面隱藏菜單,但是當我向上滾動,它顯示和隱藏多:最後,如果用戶向上滾動60像素,而隨時在頁面上我想子頭.show基於滾動位置顯示/隱藏子標題
我所用下面的代碼看我停止滾動後的時間。
JQuery的:
var newScroll = 0;
var subHeaderPosition = true;
var currentScroll = 0;
$(window).scroll(function() {
currentScroll = $(window).scrollTop();
if ($(window).width() < 779) {
if (currentScroll > 50 && subHeaderPosition) {
console.log("Current Scroll position: " + currentScroll);
console.log("Scroll should hide");
$("#sub-header").hide(300);
subHeaderPosition = false;
newScroll = $(window).scrollTop();
console.log("New Scroll position: " + newScroll);
} else if ((currentScroll - 60) < newScroll && !subHeaderPosition) {
console.log("Scroll position: " + currentScroll);
console.log("Scroll should show Sub-Header");
$("#sub-header").show(300);
subHeaderPosition = true;
newScroll = $(window).scrollTop();
} else {
newScroll = $(window).scrollTop();
}
}
});
UPDATE:增加了更多的日誌後,我現在可以告訴大家,我的newscroll和currentscroll總是碰得指向我朝着正確的方向相同的號碼。一旦我擁有了它,我會發佈一個解決方案,或者如果有人知道它,我就會全神貫注。
不幸的是,我想要「標題顯示,如果用戶開始向上滾動不只是當他們到達頁面的頂部。 – Denoteone