The Sidebar scroll up and down with the scroll bar and is always fixed in one location. Many websites and bloggers now have this function. Some shopping websites use this function for shopping cart or product category navigation, when the product page is very long, the sidebar can always play its due role. Some websites use this function to advertise the sidebar.
JQuery code:
The Code is as follows:
Var rollSet = $ ('# roll'); // check the object, # sidebar-tab is the ID to be fixed with the scroll bar, which can be changed as needed
Var offset = rollSet. offset ();
$ (Window). scroll (function (){
// Check whether the top of the object is within the visible range of the browser
Var scrollTop = $ (window). scrollTop ();
If (offset. top <scrollTop ){
RollSet. addClass ('fixed ');
} Else {
RollSet. removeClass ('fixed ');
}
});
CSS code:
The Code is as follows:
. Fixed {position: fixed; top: 20px ;}
. Fixed {_ position: absolute; _ top: expression (20 + (noValue = document.doc umentElement. scrollTop? Document.doc umentElement. scrollTop: document. body. scrollTop) + 'px ');} // compatible with IE6