I have a div which I want to become fixed at 20px from the top of the window when you scroll up and 40px from the footer when you get the bottom. My code seems inconstant, there must be a better way? Page Link
$(document).scroll(function () {
if($(window).scrollTop() >= 345){
$('#rightShipping').css({'position' : 'fixed', 'top' : '0px'});
}
if($(window).scrollTop() <= 346){
$('#rightShipping').css({'position' : '', 'top' : ''});
}
console.log($(window).scrollTop());
});
One quick idea – I would remove the
.rightColblock, leaving only the#rightShippingone withtop: 20pxand it parent withposition: relative. And then use this code:I really don’t know if this will work, as I haven’t tested it and I need to get some sleep, but I hope it helps.
Good luck!