[英]Come to old style when scrolling back
When scroll 200px my navbar will rearranged, but when scroll back to top it doesn't change. 滚动200像素时,我的导航栏将重新排列,但是滚动到顶部时,它不会改变。 I want to change my navbar to old stage when scrolling to top:
滚动到顶部时,我想将导航栏更改为旧阶段:
$(document).scroll(function() { var y = $(this).scrollTop(); if (y > 300) { $('.nav').css({"position": "fixed", "top": "0","background-color":"#252525","margin":"0","width":"100%", "padding":"10px 0 10px 0"});; } });
.nav { margin:10px 10px 10px 10px; font-size:120%; text-align:center; padding-bottom:20px; position:static; } .nav > li { display:inline-block; list-style:none; margin-right:10px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav> <img src="images/button.png" class="nav-btn"> <ul class="nav" id=""> <li><a href="file:///C:/xampp/htdocs/GEM/index.html" >HOME</a></li> <li ><a href="#" >TUTORIALS</a></li> <li><a href="#">ARTICLES</a></li> <li><a href="#">ABOUT US</a></li> </ul> </nav>
Try something like this, i have added a class .fixed
and added the properties to this class, then i use jQuery addClass()
method , this will perfectly work try with the snippet 尝试这样的事情,我添加了一个
.fixed
类,并将属性添加到该类中,然后我使用jQuery addClass()
方法,这将完美地与代码段一起尝试
$(document).scroll(function() { var y = $(this).scrollTop(); if (y > 300) { $('.nav').addClass('fixed'); } else { $('.nav').removeClass('fixed'); } });
body { height: 1000px; /* showing scroll */ } nav { margin: 10px 10px 10px 10px; font-size: 120%; text-align: center; padding-bottom: 20px; position: static; } .nav > li { display: inline-block; list-style: none; margin-right: 10px; } .fixed { position: fixed; top: 0; background-color:#252525; margin:0; width:100%; padding:10px 0 10px 0; }
<nav> <img src="images/button.png" class="nav-btn"> <ul class="nav" id=""> <li><a href="file:///C:/xampp/htdocs/GEM/index.html" >HOME</a></li> <li ><a href="#" >TUTORIALS</a></li> <li><a href="#">ARTICLES</a></li> <li><a href="#">ABOUT US</a></li> </ul> </nav> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.