javascript - Sticky Element catching on down scroll and catching on up scroll -
i have sticky side bar , right using js have catching , staying fixed t point. when scroll need catch , not go further starting point. code far.
$(window).scroll(function(e){ $el = $('.why_social'); if ($(this).scrolltop() > 735 && $el.css('position') != 'fixed'){ $('.why_social').css({'position': 'fixed', 'top': '-62px'}); } });
you should have add opposite check. should started.
$(window).scroll(function(e){ $el = $('.why_social'); if ($(this).scrolltop() > 735 && $el.css('position') !== 'fixed') { $('.why_social').css({'position': 'fixed', 'top': '-62px'}); } if ($(this).scrolltop() <= 735 && $el.css('position') === 'fixed') { { $('.why_social').css({'position': ''}); // remove `position: fixed;` } });
btw: if you've not got heart set on custom solution, see if bootstrap's affix plugin meets needs. can source code @ https://github.com/twbs/bootstrap/blob/master/js/affix.js , other link take documentation on how use it. should have add .affix { position: fixed; }
css.
Comments
Post a Comment