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)