【发布时间】:2015-08-16 13:30:46
【问题描述】:
尝试在页面滚动位置 100 像素处进行偏移以显示/隐藏导航菜单。它是在一个鼠标滚动中显示/隐藏导航,我已将 lastScroll = 0 设置为 lastScroll = 100 但不起作用。
Jquery: Fiddle
// Script
lastScroll = 0;
$(window).on('scroll',function() {
var scroll = $(window).scrollTop();
if(scroll === 0){
$(".nav").removeClass("darkHeader");
} else if(lastScroll - scroll > 0) {
$(".nav").addClass("darkHeader");
} else {
$(".nav").removeClass("darkHeader");
}
lastScroll = scroll;
});
HTML:
<div class="nav">
Sticky top navigation bar
</div>
<div class="wrap">
<h3>Some filler text</h3>
Bacon ipsum dolor sit amet mollit ball tip occaecat brisket cupidatat meatball capicola. Capicola rump turducken, elit shankle cupidatat pastrami duis fatback. Sint occaecat kielbasa labore pastrami corned beef. Sunt swine bacon, fugiat dolor aute anim jerky nostrud et venison shankle consectetur boudin landjaeger.
Pork chop sed turkey aute, duis corned beef pariatur short loin proident culpa. Capicola filet mignon fugiat corned beef shank ea, commodo doner adipisicing eu salami. Doner laboris pariatur beef ribs non id. Andouille eu meatball consectetur ham hock. Ea dolore cillum cow pork loin aliquip leberkas id est corned beef dolore t-bone. In salami jerky cupidatat et.
</div>
那么如何在鼠标滚动位置 100 像素处显示/隐藏导航。示例fiddle。
我的意思是它只在您向上滚动页面时显示导航菜单并且它以正确的方式发生。但我想在向上滚动页面 100px 后显示,意味着显示方式不正确,希望在向上滚动页面 100px 后显示和隐藏。
提前致谢。
【问题讨论】:
-
你能更详细地解释你想要做什么吗?您究竟希望导航栏何时具有“darkHeader”类?你到底想在什么时候隐藏它?尝试将描述分成几个部分:1)当滚动 100px
-
我不太明白你的问题..你能简单介绍一下吗?
-
@dangor - 是的,可以,它在 1 个鼠标滚动中显示/隐藏导航,尝试在 3 个鼠标滚动或 100px 位置显示/隐藏。
-
“1 鼠标滚动”到底是什么意思?尝试以像素表示值...
-
我的意思是显示/隐藏导航没问题,但我想在页面滚动位置 100px 后显示/隐藏。
标签: javascript jquery html scroll offset