【问题标题】:Offset to show/hide navigation menu on page scroll在页面滚动时显示/隐藏导航菜单的偏移量
【发布时间】: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


【解决方案1】:

给你。顺便说一句,medium.com 做得很好。他们甚至写了一篇关于他们是如何做到的帖子。如果您真的想完善您的解决方案,您可能需要检查一下:

https://medium.com/@mariusc23/hide-header-on-scroll-down-show-on-scroll-up-67bbaae9a78c

var lastScroll = 0;
var scrollUpStart = 0;
$(window).on('scroll',function() {

    var scroll = $(window).scrollTop();
    if(scroll === 0){
        // we are at the top
        $(".nav").removeClass("darkHeader");
    } else if(lastScroll > scroll) {
        // we are scrolling up

        // we check if we have started scrolling up already
        if (scrollUpStart < scroll) {
            // if we just started scrolling up, we set 
            // the scrollUpStart to the current scroll value
            scrollUpStart = scroll;
        }

        // if we continue scrolling up, the difference between 
        // scrollUpStart and scroll will eventually reach a 100px
        if (scrollUpStart - scroll > 100) {
            $(".nav").addClass("darkHeader");
        }

    } else {
        // we are scrolling down
        // reset scrollUpStart
        scrollUpStart = 0;
        $(".nav").removeClass("darkHeader");
    }
    lastScroll = scroll;
});

【讨论】:

  • 谢谢,但 .nav 不像我以前那样隐藏,我想保持旧的隐藏或显示,而你的 100 偏移量不起作用。请看:jsfiddle.net/9fbr320y/17
  • 它不工作,因为你改变了你原来的小提琴。最初你在darkHeader 类上拥有position:fixed 而不是nav 类:jsfiddle.net/9fbr320y/1 在你的第二个小提琴中,你将position: fixed 放在nav 类上而不是darkHeader 类上:跨度>
  • 这是你原来的小提琴,用我的答案更新:jsfiddle.net/9fbr320y/18
  • 嗨,它仍然偏移 0px ,它应该在 100px 位置或 3 次鼠标滚动时显示和隐藏。
  • 我放弃了,还是不明白你到底想要什么。
【解决方案2】:

这个 JS 应该可以工作 :)

Javascript

$(window).on('scroll',function() {
    var scroll = $(window).scrollTop();
    if(scroll >= 100){
        $('.nav').fadeOut();
    } else {
        if(!$('.nav').is(":visible")){
            $('.nav').fadeIn();
        }
    }
});

http://jsfiddle.net/9fbr320y/16/

【讨论】:

  • 我不认为这只是隐藏/显示滚动的问题,还包括添加/删除“darkHeader”类
  • @Sofiene DJEBALI - 嗨,谢谢,但不是这样。我的意思是我的旧显示/隐藏导航没问题,但我想在 100px 页面滚动位置后显示/隐藏而不是 0px。
  • @SofieneDJEBALI - 谢谢,细节是:我的旧隐藏和显示不应该改变它只在您向上滚动页面时显示导航菜单并且它以正确的方式发生。但我想在向上滚动页面 100px 后显示,意味着没有以正确的方式显示,想在向上滚动页面 100px 后显示。对不起我的英语不好。
【解决方案3】:

$(window).scrollTop() 返回顶部的滚动量(以像素为单位),因此只需编写:

$(window).on('scroll',function() {    
    console.log($(window).scrollTop());
    var scroll = $(window).scrollTop();
    if(scroll > 100){
        $(".nav").addClass("darkHeader");
    } else {
        $(".nav").removeClass("darkHeader");
    } 
});

【讨论】:

  • 嗨,谢谢,但不是这样。我的意思是旧的显示/隐藏导航还可以,但我想在页面滚动位置 100 像素后显示/隐藏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-06
  • 1970-01-01
相关资源
最近更新 更多