Follow the special effect code on the top of the jQuery navigation bar

Source: Internet
Author: User

JQ is easy to implement.

I have a method here. Let's talk about it in an article on the air and water? (? ^ ?)?

Step 1

Reference jQuery in the head label first. If jQuery has already been referenced, you don't need to repeat it.

<Script type = "text/javascript" src = "http://libs.baidu.com/jquery/1.8.0/jquery.js"/>

Step 2

Then modify the div in the navigation bar

Assign the id of the div in the navigation bar to id = "nav". If the original wood has an id, add it.

For example

The code is as follows: Copy code


<Div class = "nav"> content in the navigation bar </div>
 
Change

<Div class = "nav" id = "nav"> content in the navigation bar </div>

Of course, to prevent conflicts, if the navigation bar div already has an id, you can set another div with id = "nav" outside.

Step 3

Add the header label

The code is as follows: Copy code

<Script type = "text/javascript">
$ (Document). ready (function (){
$ (Window). scroll (function (){
Var t = document. body. scrollTop | document.doc umentElement. scrollTop;
If (t> XXXXX ){
$ ("# Nav" ).css ({
"Position": "fixed ",
"Top": "0px ",
"Left": "0px"
});
} Else {
$ ("# Nav" ).css ({
"Position ":"",
"Top ":"",
"Left ":""
});
             } 
});
});
</Script>

Step 4

Calculate the distance from the bottom of the navigation bar to the top of the page.

For example, my structure is

The code is as follows: Copy code


<Body>
<Header>
<Div class = "head" style = "height: 100px;"> </div>
<Div id = "nav" style = "height: 100px;"> </div>
</Header>
<Div class = "content"> </div>
<Footer> </footer>
</Body>

The distance from the bottom of the navigation bar to the top of the page is the height of the head layer + the height of the navigation bar = 200px

Finally, change XXXXX in the fifth line of the code provided in step 3 to the distance from the bottom of the navigation bar to the top of the page.

As in the preceding example, you can change XXXXX to 200 (no additional unit is required)

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.