scroll fixed block
by AJIEKCEU
HTML
<div id="wrapper">
<div id="left">
<div id="sidebar">
Sidebar Content
</div>
</div>
<div id="right">
This is the text of the main part of the page.
</div>
<div class="clear"></div>
</div>
<div id="footer">Footer</div>
CSS
#header { background: #c2c2c2; height: 50px ;}
#wrapper {
width: 500px
height:auto;
position:relative;
}
#left {
background: #d7d7d7;
position:absolute;
width: 150px;
height:100%;
}
#right {
position: relative;
width: 350px;
float: right;
height:600px;
}
#sidebar {
background: #0096d7;
width: 150px;
color: #fff
}
.clear {
clear: both
}
#footer {
background: #c2c2c2;
height:800px;
}
JavaScript
var length = $('#left').height() - $('#sidebar').height() + $('#left').offset().top;
$(window).scroll(function () {
var scroll = $(this).scrollTop();
var height = $('#sidebar').height() + 'px';
if (scroll < $('#left').offset().top) {
$('#sidebar').css({
'position': 'absolute',
'top': '0'
});
} else if (scroll > length) {
$('#sidebar').css({
'position': 'absolute',
'bottom': '0',
'top': 'auto'
});
} else {
$('#sidebar').css({
'position': 'fixed',
'top': '0',
'height': height
});
}
});