jquery - Sliding sidebar position calculations not correct -


i'm building follow or sticky sidebar in jquery website i'm working on.

here's link site sidebar.

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

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -