[英]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 文件中注意到的第一件事是您的样式规则中的
relative
和absolute
位置,请删除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
因为lastScroll
从0
开始并且scroll
从0
开始计数,你的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.