×

Welcome to TagMyCode

Please login or create account to add a snippet.
1
0
 
1
Language: Javascript
Posted by: Massimo Zappino
Added: Jul 17, 2012 10:03 AM
Modified: Aug 11, 2017 2:35 PM
Views: 339
This snippet allows you to get a div that follows the scroll page, it uses jQuery
  1. <script type="text/javascript">
  2. var el = $('#my-scrollable-div');
  3. var elpos = el.offset().top;
  4. $(window).scroll(function () {
  5.     var y = $(this).scrollTop();
  6.     if (y < elpos) {
  7.         el.stop().animate({'top': 0}, 200);
  8.     }
  9.     else{
  10.         el.stop().animate({'top': y-elpos}, 200);
  11.     }
  12. });
  13. </script>
  14.  
  15.  
  16. <div id="my-scrollable-div" style="position: relative;">
  17.     ...
  18. </div>
  19.  
  20.