I have the Javascript below that is used to make a sidebar widget stick to a fixed position once the page is scrolled down, once you scroll to the bottom of the page where the footer div is I then un-sticky it so that the stickied div does not overlap the footer.
It works great but I am not very good with Javascript, I am hoping to clean it up because it looks sloppy, any help from a Guru?
The Javascript
$(window).load(function () {
/*-----------------------*/
* Fixed Widget on Scroll
*-----------------------*/
// set some Div vars
var stickyID = $("#sticky-scroll-wrapper"),
footerID = $('#footer'),
stickyDivHeight = stickyID.outerHeight(),
stickyIDMargin = 70;
$(window).scroll(function () {
var currentPosition = $(document).scrollTop() + stickyIDMargin;
var stickyIDPosition = stickyID.offset().top;
// cut off point/un sticky when we reach this far down the page
var limit = footerID.offset().top - stickyDivHeight;
//determines whether sidebar should stick and applies appropriate settings to make it stick
if (currentPosition >= stickyIDPosition && currentPosition < limit) {
$('#sticky-elements').css({
'position': 'fixed',
'top': '70px'
});
} else {
$('#sticky-elements').css({
'position': 'static',
});
}
// Un-stick once we reach the #fotter section of the page to prevent overlapping
if (limit < currentPosition) {
var diff = limit - currentPosition;
$('#sticky-elements').css({
'top': diff + stickyIDMargin,
'position': 'fixed',
});
}
});
});
The HTML
<div id="sticky-scroll-wrapper">
<div id="sticky-elements" style="position: static; ">sidebar content</div>
</div>