【发布时间】:2016-11-29 04:57:11
【问题描述】:
问题:我正在尝试完成以下动画:当页面在移动设备中加载时,我想显示 ID 为“子标题”的 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();
}
}
});
更新:添加更多日志后,我现在可以看出我的新闻滚动和当前滚动总是以相同的数字结束,这为我指明了正确的方向。一旦我有了它,我会发布一个解决方案,或者如果有人发现它,我会全神贯注。
【问题讨论】:
标签: javascript jquery html css