Gets the element (here positioned element a) from the top of the height, and then sets the scroll scrolling event, such as exceeding that height, the position of a is set to fixed, less than the height, modified back to relative.
Method One:
$ (function() { var elm = $ ('. Nav '); var startpos = $ (elm). Offset (). Top; function () { var p = $ (window). scrolltop (); $ (Elm). css (' position ', ((P) > Startpos)? ' Fixed ': ' Static '); $ (Elm). CSS (' Top ', ((P) > Startpos)? ' 0px ': ');
Method Two:
$(function(){//gets the distance to locate the element from the top of the browservarNAVH = $ (". Nav")). Offset (). Top;//scroll bar Events$ (window). Scroll (function(){//get the sliding distance of the scroll barvarScroh = $ ( This). scrolltop ();//the sliding distance of the scrollbar is greater than or equal to the distance of the anchor element from the top of the browser, fixed, and not fixedif(scroh>=NAVH) {$(". Nav"). css ({"Position": "Fixed", "top": 0});}Else if(SCROH
". Nav"). css ({"position": "Static"});}})})
Cases:
Article from: http://blog.sina.com.cn/s/blog_3eba8f1c0101k94s.html
Jquery Div Scroll to top of browser position fixed