jquery - Javascript for sticky function not working -
i'm trying create standard sticky-function bottom side navigation. it's low functioning tool, making absolutely positioned element 300 px top of window bottom of page. should easy. i've written following code below
css
.style2info { margin-top:170px; height: 3800px; background-color: #6b6b6b; margin-left: 59px; width: 70px; position: relative; } .anchornav { height: 30px; width: 25px; position: absolute; } .anchornav.sticky { position: fixed; top: 0px; } javascript
$(document).ready(function () { var win = $(window), fxel = $('.anchornav'), eloffset = fxel.offset().top; win.scroll(function() { if (win.scrolltop() > eloffset) { fxel.addclass("sticky"); } else { fxel.removeclass("sticky"); } }); html
<div class="style2info"><!-- sticky --> <div class="anchornav"> <p>sticky?</p></div> </div> on fiddle, it's working perfectly. problem on actual website, not much. http://webstage.emich.edu/dining-new/locations.php (ignore inline styles, it's long i'm still working on javascript)
the thing can think of way it's located on page, larger div ("style2info") has negative margin-top position correctly. possible that messing code? or else?
any appreciated! thank you!!!!
******i have read other stackoverflows regarding sticky objects. problem they're either taking github, or attempting other things. mine straight , simple , i'd learn i'm doing wrong rather using other programs it**
so after careful checking , trying figure out wrong, found serve less forgiving jsfiddle. reformatted javascript following
function sticky_relocate() { var window_top = $(window).scrolltop(); var div_top = $('.style2info').offset().top; if (window_top > div_top) $('.anchornav').addclass('sticky') else $('.anchornav').removeclass('sticky'); } $(function() { $(window).scroll(sticky_relocate); sticky_relocate(); }); works now!
Comments
Post a Comment