I got a block of content:
<div id="content_holder">
<div id="fixed_content"></div>
<div id="bottom_content"></div>
</div>
I want to achieve this - when i scroll down with my mouse, the fixed_content
block would turn fixed position and will stick on top of page, until i scroll down to bottom_content
.
So far i got this:
var top_positio_saver = $('#fixed_content').offset().top;
$(document).on('scroll', function() {
if ($('#fixed_content').offset().top < $(document).scrollTop()) {
$('#fixed_content').css('position', 'fixed');
$('#fixed_content').css('top', 0);
}
if ($(document).scrollTop() < top_positio_saver) {
$('#fixed_content').css('position', 'static');
$('#fixed_content').css('top', 0);
$('#fixed_content').css('margin-top', 0);
}
});
With this code, the fixed_content
block is fixed once i scroll down enough, and turns back to static once i scroll up enough. The problem here is that if i scroll down too much, this block goes on top of bottom_content
block, instead i want it to stop near the bottom_content block.
Any suggestions?