Home  >  Article  >  Web Front-end  >  Jquery implements fixed sidebar following scroll bar (compatible with IE6)_jquery

Jquery implements fixed sidebar following scroll bar (compatible with IE6)_jquery

WBOY
WBOYOriginal
2016-05-16 16:53:461326browse

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:

Copy 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:
Copy code The code is as follows:

.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
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn