Jquery scrollTop method hide top navigation bar based on scrolling pixel display

Source: Internet
Author: User

This article uses the scrollTop method of jquery to monitor the vertical scrolling pixels of the page and hide or display the navigation bar at the top of the page based on the pixels.
Effect preview URL: http://www.keleyi.com/keleyi/phtml/scrolloversee.htm

ScrollTop introduction:

Syntax
$ (Selector). scrollTop (offset)

Parameters
Offset
Description
Optional. Specifies the offset relative to the top of the scroll bar, in pixels.

Definition and usage
The scrollTop () method returns or sets the vertical position of the scroll bar that matches the element.
Scroll top offset refers to the offset of the scroll bar relative to its top.
If no parameter is set for this method, the offset relative to the top of the scroll bar in pixels is returned.

Tips and comments
Note: This method is valid for both visible and invisible elements.
Note: When used to obtain a value, this method returns only the scroll top offset of the First Matching Element.
Note: When used to set the value, this method sets the scroll top offset of all matching elements.

Complete code is provided and saved to an html file. You can use the following code to access the html file:
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.keleyi.com/1999/xhtml">
<Head>
<Title> monitor scroll pixels, display or hide the top navigation bar-ke leyi </title>
<Style type = "text/css">
# Topmenu_keleyi_com {position: fixed; top: 0px; left: 0px; right: 0px; width: 800px; margin-left: auto; margin-right: auto; height: 50px; background-color: transparent; display: none ;}
# Topmenu_keleyi_com li {float: left;
List-style: none;
Margin-right: 1px;
Width: pixel PX;
Background-color: # e0f0e0;
}
# Topmenu_keleyi_com ul {margin: 0px; padding: 0px ;}
Body {margin: 0px ;}
</Style>
<Script type = "text/javascript" src = "http://www.keleyi.com/keleyi/pmedia/jquery-1.9.1.min.js"> </script>
</Head>
<Body>
<Div id = "topmenu_keleyi_com"> <ul> <li> <a href = "http://www.keleyi.com/menu/net/" target = "_ blank">. NET </a> </li> <a href = "http://www.keleyi.com/menu/javascript/" target = "_ blank"> Javascript </a> </li> <Div style = "background-color: #959822; width: 100%; height: 150px;"> scroll down the page </div>
<Div style = "background-color: Green; width: 100%; height: 150px;"> www.keleyi.com </div>
<Div style = "background-color: Red; width: 100%; height: 150px;"> welcome </div>
<Div style = "background-color: Yellow; width: 100%; height: 150px;"> hi </div>
<Div style = "background-color: Silver; width: 100%; height: 150px;"> ke leyi </div>
<Div style = "background-color: Aqua; width: 100%; height: 150px;"> keleyi.com </div>
<Div style = "background-color: Fuchsia; width: 100%; height: 150px;"> keleyi </div>
<Div style = "background-color: Green; width: 100%; height: 150px;"> keleyi.com </div>
<Div style = "background-color: Blue; width: 100%; height: 150px;"> ke leyi </div>
<Div style = "background-color: Olive; width: 100%; height: 150px;"> ke leyi monitors downloading pixels </div>
<Div style = "background-color: Green; width: 100%; height: 150px;"> A </div>
<Div style = "background-color: Purple; width: 100%; height: 150px;"> jquery </div>
<Div style = "background-color: Green; width: 100%; height: 150px; "> <a href =" http://www.keleyi.com/a/bjac/f4a6f78d74a251c5.htm "target =" _ blank "> original </a> </div>
<Div style = "background-color: Lime; width: 100%; height: 150px;"> keleyi.com </div>
<Div style = "background-color: Orange; width: 100%; height: 150px;"> complete code </div>
<Script type = "text/javascript">
$ (Function (){
// When the position of the scroll bar is below 200 pixels from the top, the top navigation appears; otherwise, it disappears.
$ (Function (){
$ (Window). scroll (function (){
If ($ (window). scrollTop ()> 200 ){
$ ("# Topmenu_keleyi_com"). fadeIn (100 );
}
Else {
$ ("# Topmenu_keleyi_com"). fadeOut (100 );
}
});
});
});
</Script>
</Body>
</Html>

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.