javascript - Make sticky/fixed element stop at footer -
i'm trying make side bar stop following user's scroll once hits footer. right set z-index -2
goes behind footer, sticks out tiny bit.
javascript
$(document).ready(function () { var floatingdiv = $('#side_bar'); var floatingdivposition = floatingdiv.position(); $(window).scroll(function (){ var scrollbarposition = $(window).scrolltop(); if(scrollbarposition >= floatingdivposition.top) { floatingdiv.css({ 'position': 'fixed', 'top': 55, 'width': '18.6676%', 'z-index': -2 }); } else{ floatingdiv.css({ 'position': 'relative', 'top': 0, 'width': '79.4392%' }); } }); });
html
<div id="content"> <div id="col_1"> <div id="side_bar"> <h4 id="cater_to">we cater to</h4> <a href="#"><button class="side_bar_btns">contractor</button></a> <a href="#"><button class="side_bar_btns">wood/sport floors</button></a> <a href="#"><button class="side_bar_btns">grocery/commercial</button></a> <a href="#"><button class="side_bar_btns">education</button></a> <h4 id="simplicity">simplicity</h4> <div id="all_systems_side_bar"> <img src="images/all_systems_logo.png" alt="all systems maintenance logo. links more systems maintenance." width="100%"> </div><!-- systems side bar --> </div><!-- side_bar --> </div><!-- col_1 --> <div id="col_2"> //// bunch of stuff here </div><!-- col_2 --> <div class="clear"></div> </div><!-- content --> <footer> /// bunch of stuff here </footer>
css
#col_1 { float:left; margin-top:44px; width:23.4994%; margin-left:3.9531%; } #side_bar { background:#003768; min-height:665px; width:79.4392%; border-radius:20px; box-shadow: 10px 10px 5px #888; } #col_2 { float:right; margin-top:44px; width:68.5944%; margin-right:3.9531%; } footer { background-image:url(../images/foot_background.gif); background-repeat:no-repeat; background-size:cover; }
the footer background same height screen (about 824px when inspect chrome).
found gem on youtube @ https://www.youtube.com/watch?v=5s0l6dcvevk , changed come following, works.
$(function() { if ($('#side_bar').length) { var el = $('#side_bar'); var stickytop = $('#side_bar').offset().top; var stickyheight = $('#side_bar').height(); $(window).scroll(function(){ var limit = $('footer').offset().top - stickyheight - 20; var windowtop = $(window).scrolltop(); if (stickytop < windowtop) { el.css({ position: 'fixed', top: 55, width: '18.6676%' }); } else { el.css({ position: 'static', width: '79.4392%' }); } if (limit < windowtop) { var diff = limit - windowtop; el.css({ top: diff }); } }); } });
Comments
Post a Comment