简体   繁体   English

缩小粘性导航栏

[英]Shrinking Sticky Navigation Bar

I have the following code on my site for my nav bar: http://jsfiddle.net/faj0o4cq/ 我的导航栏在我的网站上有以下代码: http : //jsfiddle.net/faj0o4cq/

It works great without any of the margin-top parts, but when I add them in it breaks. 它在没有任何margin-top部分的情况下都很margin-top ,但是当我添加它们时,它就会中断。 See how this works fine: http://jsfiddle.net/faj0o4cq/1/ 看看它如何正常工作: http//jsfiddle.net/faj0o4cq/1/

What am I doing wrong in my Javascript? 我的JavaScript到底在做什么错?

 $(function(){ $('#header_nav').data('size','big'); }); $(window).scroll(function(){ if($(document).scrollTop() > 0) { if($('#header_nav').data('size') == 'big') { $('#header_nav').data('size','small'); $('#header_nav').stop().animate({ height:'78px' },600); $('header nav').stop().animate({ margin-top:'50px' },600); } } else { if($('#header_nav').data('size') == 'small') { $('#header_nav').data('size','big'); $('#header_nav').stop().animate({ height:'100px' },600); $('header nav').stop().animate({ margin-top:'100px' },600); } } }); 
 #header_nav { background:blue; height:100px; position:fixed; top:0 } body { height:9000px } 
 <header> <div id="header_nav"> <nav>nav here</nav> </div> </header> 

You have to enclose margin-top in quotation marks. 您必须在页margin-top加上引号。

Every time, if something related to JavaScript does not work, check the console. 每次,如果与JavaScript相关的某些功能不起作用,请检查控制台。 It may give you some hints on what is wrong. 它可能会给您一些提示,说明出了什么问题。

 $(function(){ $('#header_nav').data('size','big'); }); $(window).scroll(function(){ if($(document).scrollTop() > 0) { if($('#header_nav').data('size') == 'big') { $('#header_nav').data('size','small'); $('#header_nav').stop().animate({ height:'78px' },600); $('header nav').stop().animate({ 'margin-top':'50px' },600); } } else { if($('#header_nav').data('size') == 'small') { $('#header_nav').data('size','big'); $('#header_nav').stop().animate({ height:'100px' },600); $('header nav').stop().animate({ 'margin-top':'100px' },600); } } }); 
 #header_nav { background:blue; height:100px; position:fixed; top:0 } body { height:9000px } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <header> <div id="header_nav"> <nav>nav here</nav> </div> </header> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM