Some shopping websites use this function to place shopping cart or product category navigation, so that when the product page is very long, the sidebar can always play its due role. Some websites use this function to place ads in the sidebar.
jQuery code:
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:
.fixed{ position:fixed; top:20px;}
.fixed{_position:absolute; _top:expression((20 (noValue = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)) 'px' );}//Compatible with IE6