html - Make a div fixed at particular height -
in web layout using left side bar , content div.both of them want scroll @ point when left side bar content has finished left side bar div div has come fixed , content alone want scroll
you need use javascript or in example jquery.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script> $(window).scroll(function(e) { var divtop = $("#div-name").offset().top, stoppoint = 400; if ($(this).scrolltop() >= (divtop + stoppoint) && $('.scroller').css('position') != 'fixed') { $('#div-name').css({ 'position': 'fixed', 'top': '0px', 'height': '300px', 'overflow-y': 'scroll' }); } }); </script> <style> #div-name { margin-top:45px; padding:15px; width:250px; height:1200px; background-color:#ccc; } </style>
the first line calls jquery library
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
the following script finds div it's id , gets it's top point. stop point how far want div scroll before stopping.
the if statement kicks in when screen scrolls past stop point , alters css set position fixed plus else want do.
hope helps.
Comments
Post a Comment