jquery - Sliding sidebar position calculations not correct -
i'm building follow or sticky sidebar in jquery website i'm working on.
you can see it's behavior strange. i'm trying follow guide , add stop function once gets bottom:
http://css-tricks.com/scrollfollow-sidebar/
when user scrolls down, want sidebar remain @ top 32px top-padding, , when user scrolls bottom, want sidebar stop 32px bottom-padding @ top of footer. i'm confused variables , math behind it, in head makes sense, maybe need else take @ it.
anyways, here's jquery code:
<script> $(function() { var $sidebar = $("#sidebar"), $window = $(window), $footer = $("footer"), // use footer id here foffset = $footer.offset(), offset = $sidebar.offset(), threshold = foffset.top - $sidebar.height(), toppadding = 15; $window.scroll(function() { if ($window.scrolltop() > threshold) { $sidebar.stop().animate({ margintop: threshold }); } else if ($window.scrolltop() > offset.top) { $sidebar.stop().animate({ margintop: $sidebar.height() - offset.top + toppadding }, 400); } else { $sidebar.stop().animate({ margintop: 0 }, 400); } }); }); </script>
and css sidebar id:
#sidebar { position: fixed !important; }
thanks in advance.
try include padding calculation:
threshold = foffset.top - $sidebar.outerheight(),
Comments
Post a Comment