javascript - How to make a fixed sidebar stop at the end of a container -
i have container 2 columns, 1 of holds sidebar.
the sidebar fixed, , when gets near bottom used jquery alter bottom
have stay @ bottom of container.
how can make stops moving when hits bottom of container (outlined in red)? have work sidebar of height.
html
<div class="container"> <div class="col-left"> <div class="sidebar"> fixed sidebar </div> </div> <div class="col-right"> <p>paragraph</p> <p>paragraph</p> <p>paragraph</p> </div> </div> <footer>footer</footer>
css
.container { outline: 1px solid red; position: relative; } .col-left { width: 58%; display: inline-block; outline: 1px solid black; vertical-align: top; height: 100%; } .col-right { width: 40%; display: inline-block; outline: 1px solid black; vertical-align: top; } .sidebar { position: fixed; top: 50px; left: 50px; height: 200px; width: 100px; background: #fff; } footer { background: #000; width: 100%; height: 300px; margin-top: 100px; color: #fff; }
jquery (doubt it'll of use think needs rethought)
jquery(window).scroll(function(){ var scrollbottom = jquery(document).height() - jquery(this).scrolltop() - jquery(this).height(); console.log(scrollbottom); if (scrollbottom < 300){ jquery('.sidebar').css('bottom', math.abs(scrollbottom - 420)); jquery('.sidebar').css('top', 'auto'); } else { jquery('.sidebar').css('bottom', 'auto'); jquery('.sidebar').css('top', '50px'); }
here's jquery solution whipped calculating heights of relevant elements, , factoring in current scroll position of page. when sidebar move outside of container, .lock
class added unfixes css.
working example below:
var $sidebar = $('.sidebar'); var $container = $('.container'); var sidebottom = $sidebar.offset().top + $sidebar.height(); var contbottom = $container.offset().top + $container.height(); $(window).scroll(function() { if (window.scrolly + sidebottom > contbottom) { $sidebar.addclass('lock'); } else if ($sidebar.hasclass('lock')) { $sidebar.removeclass('lock'); } });
body { background: #eee; margin: 0; } .container { outline: 1px solid red; position: relative; } .col-left { width: 58%; display: inline-block; outline: 1px solid black; vertical-align: top; height: 100%; } .col-right { width: 40%; display: inline-block; outline: 1px solid black; vertical-align: top; } .sidebar { position: fixed; top: 50px; left: 50px; height: 140px; width: 100px; background: #fff; } .sidebar.lock { position: absolute; bottom: 0; top: auto; } footer { background: #000; width: 100%; height: 400px; margin-top: 100px; color: #fff; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="col-left"> <div class="sidebar"> fixed sidebar </div> </div> <div class="col-right"> <p>paragraph</p> <p>paragraph</p> <p>paragraph</p> <p>paragraph</p> <p>paragraph</p> <p>paragraph</p> <p>paragraph</p> <p>paragraph</p> <p>paragraph</p> <p>paragraph</p> <p>paragraph</p> </div> </div> <footer>footer</footer>
Comments
Post a Comment