Display and hide the top navigation bar when scrolling pages based on jquery.
This example describes the code for hiding effects in the top navigation bar when jquery implements Page scrolling. Share it with you for your reference. The details are as follows:
The running effect is as follows:
The Code is as follows:
Introduce core files
<script src="js/jquery/1.11.1/jquery.min.js"></script>
Construct html,The div 'margint' is created to display the scroll bar, and has no practical effect.
<Div class = "top-title"> This is the top navigation bar </div> <div class = "margint"> <p> scroll to see the effect </p> <p> scroll to see effect </p> </div>
Write CSS
.top-title {background:#e74c3c;color:white;font-size:24px;padding:5px;text-align:center;position: fixed;left:0;top:0;width:100%;transition: top .5s;}.hiddened{top: -90px;}.showed{top:0;z-index: 9999;}
The transition: top. 5 s; defined in top-title refers to the change of the top-direction value of the animation display in. 5S. For example, after the hidden class is added, top-title will buffer the top 0 animation from-90PX within S.
Write JS
$ (Function () {var winHeight = $ (document ). scrollTop (); $ (window ). scroll (function () {var scrollY = $ (document ). scrollTop (); // obtain the vertical scroll distance, that is, the number of Scrolls if (scrollY> 550) {// hide if the scroll distance is greater than 550px, otherwise, the hidden class $ ('. top-title '). addClass ('dendened');} else {$ ('. top-title '). removeClass ('dendened');} if (scrollY> winHeight) {// if it is not scrolled to the top, delete the display class. Otherwise, add the display class $ ('. top-title '). removeClass ('showed');} else {$ ('. top-title '). addClass ('showed ');}});});
The above is the overall idea of displaying and hiding the top navigation bar when pages are scrolling Based on jquery. I hope you can follow this idea to display the hidden effect of navigation. Thank you for reading.