Display and hide the top navigation bar when scrolling pages based on jquery.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.