简体   繁体   English

向下滚动时隐藏导航栏,并在用户使用 jquery 向上滚动页面时显示它,不能正常工作

[英]Hide navigation bar on scrolling down and show it when user scroll the page up using jquery, doesn't work quite right

I want to hide the navigation bar when a user scroll down the page and show it when the user scroll the page up.我想在用户向下滚动页面时隐藏导航栏,并在用户向上滚动页面时显示它。 I 'm using the code below and it works ok.我正在使用下面的代码,它工作正常。 The ONLY problem i have is that the "else" part of the statement doesn't work.我唯一的问题是语句的“else”部分不起作用。 To be more specific, when the page reach at the top again the "navigation-bar" div should be positioned absolute and have 20px padding on top and bottom.更具体地说,当页面再次到达顶部时,“导航栏”div 应该绝对定位,并且在顶部和底部有 20px 的填充。 What i 'm doing wrong?我做错了什么?

 lastScroll = 0; $(window).on('scroll',function() { var scroll = $(window).scrollTop(); if(lastScroll - scroll > 0) { $(".top-navbar").css("padding-top","0px"); $(".top-navbar").css("padding-bottom","0px"); $(".top-navbar").css("background","red"); $(".top-navbar").css("position","fixed"); $(".top-navbar").css("top","0px"); } else { $(".top-navbar").css("padding-top","20px"); $(".top-navbar").css("padding-bottom","20px"); $(".top-navbar").css("background","#00bbcf"); $(".top-navbar").css("position","absolute"); $(".top-navbar").css("top","0px"); } lastScroll = scroll; });
 .top-navbar { width: 100%; z-index: 1; position: relative; position:absolute; top:0px; left:0; padding-top: 20px; padding-bottom:20px; background:#00bbcf; color:#fff; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <div class="top-navbar">Navigation Bar</div> <div class="dummy-content"> Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book. It usually begins with: “Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.” The purpose of lorem ipsum is to create a natural looking block of text (sentence, paragraph, page, etc.) that doesn't distract from the layout. A practice not without controversy, laying out pages with meaningless filler text can be very useful when the focus is meant to be on design, not content. The passage experienced a surge in popularity during the 1960s when Letraset used it on their dry-transfer sheets, and again during the 90s as desktop publishers bundled the text with their software. Today it's seen all around the web; on templates, websites, and stock designs. Use our generator to get your own, or read on for the authoritative history of lorem ipsum. Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book. It usually begins with: “Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.” The purpose of lorem ipsum is to create a natural looking block of text (sentence, paragraph, page, etc.) that doesn't distract from the layout. A practice not without controversy, laying out pages with meaningless filler text can be very useful when the focus is meant to be on design, not content. The passage experienced a surge in popularity during the 1960s when Letraset used it on their dry-transfer sheets, and again during the 90s as desktop publishers bundled the text with their software. Today it's seen all around the web; on templates, websites, and stock designs. Use our generator to get your own, or read on for the authoritative history of lorem ipsum. Lorem ipsum, or lipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of Cicero's De Finibus Bonorum et Malorum for use in a type specimen book. It usually begins with: “Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.” The purpose of lorem ipsum is to create a natural looking block of text (sentence, paragraph, page, etc.) that doesn't distract from the layout. A practice not without controversy, laying out pages with meaningless filler text can be very useful when the focus is meant to be on design, not content. The passage experienced a surge in popularity during the 1960s when Letraset used it on their dry-transfer sheets, and again during the 90s as desktop publishers bundled the text with their software. Today it's seen all around the web; on templates, websites, and stock designs. Use our generator to get your own, or read on for the authoritative history of lorem ipsum. </div>

You need to check current top position:您需要检查当前的顶级 position:

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

    if((lastScroll - scroll) > 0 && scroll > 56) {
        $(".top-navbar").css("padding-top","0px");
        $(".top-navbar").css("padding-bottom","0px");
        $(".top-navbar").css("background","red");
        $(".top-navbar").css("position","fixed");
        $(".top-navbar").css("top","0px");        
    } else {
        $(".top-navbar").css("padding-top","20px");
        $(".top-navbar").css("padding-bottom","20px");
        $(".top-navbar").css("background","#00bbcf");
        $(".top-navbar").css("position","absolute");
        $(".top-navbar").css("top","0px");
    }

    lastScroll = scroll;
});

scroll > 56, because its paddings 20+20 and font-size: 16. This may not be entirely correct, but approximately. scroll > 56,因为它的填充 20+20 和 font-size: 16。这可能不完全正确,但大致正确。

First thing I noticed in your CSS file was the relative and absolute positions within your style rule, please remove the position: relative .我在您的 CSS 文件中注意到的第一件事是您的样式规则中的relativeabsolute位置,请删除position: relative The problem with your code your condition lastScroll - scroll > 0 since lastScroll starts from 0 and scroll counts up from 0 your else block runs before you need it hence the unexpected behavior.你的代码问题你的条件lastScroll - scroll > 0因为lastScroll0开始并且scroll0开始计数,你的else块在你需要它之前运行,因此会出现意外行为。 you can fix it by reversing the condition like this scroll - lastScroll > 0 .您可以通过像这样反转条件来修复它scroll - lastScroll > 0 I debugged it on code pen already and it works that way.我已经在代码笔上调试过它,它就是这样工作的。 #cheers #干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的导航栏在向下滚动时不隐藏而在向上滚动时出现? - Why doesn't my nav bar hide when scrolling down and appear when scrolling up? 在滚动时显示元素(向上和向下) - 当页面为 static(不滚动)时隐藏元素 - Show element on scroll (both up and down) - hide element when page is static (not scrolling) 如何在向下滚动时隐藏div,然后向上滚动显示 - How to hide div when scrolling down and then show scroll up 如何在向下滚动时隐藏div,然后在向上滚动时显示div? - How to hide div when scrolling down and then show it as you scroll up? 用户向上/向下滚动时如何隐藏/显示导航栏 - How to hide/show nav bar when user scrolls up/down facebook应用程序样式向下滚动时隐藏标题栏,向上滚动时再次显示 - facebook app style hide header bar when scrolling down, show again when scrolling up 想要在使用 js 或 jquery 向下/向上滚动时隐藏/显示导航栏 - want to hide/show navbar when scroll down/up using js or jquery 向上或向下滚动时显示/隐藏页脚 - Show/Hide footer when scrolling up or down 我有一个导航栏,当您向下滚动时,它会变得固定。 除非您向下滚动,否则它不起作用 - I have a navigation bar that becomes fixed when you scroll down. It doesn't work unless you're scrolled down JQuery - 连续滚动导航,除了它不太有效 - JQuery - A continuous rolling navigation, except it doesn't quite work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM