I've created a function that changes the 'position' of a DIV to 'fixed' after a certain point of scroll. The purpose is to keep it visible on the screen while page is scrolled. Works great (although not in IE6).
Problem is that when I get to the bottom of a page, I don't want the DIV to overlap the footer. I'm guessing I'll need to change the 'position' from 'fixed' to 'absolute', setting the 'bottom' property to something slightly greater then the height of the footer.
It's best to test this issue on this page. Make sure that your window is resized a littler smaller so that you can see the sidebar overlapping the footer.
Here's my code so far :
$(document).ready(function(){
var element = $("#sidebar");
var window_height = $(window).height();
var element_height = element.height();
// On scroll, set or unset the fixed position
$(window).scroll(function() {
if (window_height > element_height) {
if ($(document).scrollTop() > 220) {
element.css("position","fixed");
element.css("top","9px");
element.css("padding-top","0");
}
else {
element.css("position","relative");
element.css("top","0");
element.css("padding-top","57px");
}
}
else {
element.css("position","relative");
element.css("top","0");
element.css("padding-top","57px");
}
});
// On window resize, set or unset the fixed position
$(window).resize(function(){
window_height = $(window).height();
if (window_height > element_height) {
if ($(document).scrollTop() > 220) {
element.css("position","fixed");
element.css("top","9px");
element.css("padding-top","0");
}
else {
element.css("position","relative");
element.css("top","0");
element.css("padding-top","57px");
}
}
else {
element.css("position","relative");
element.css("top","0");
element.css("padding-top","57px");
}
});
});