jQuery Style How to - Update bottom offset as scrolling the window








Question

We would like to know how to update bottom offset as scrolling the window.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from  w w  w  .java  2s.com-->
    $(window).scroll(function() {
        console.log($(this).scrollTop());
        $('#myElement').css('bottom', -1*$(this).scrollTop() + "px");
    });
});
</script>
</head>
<body>
  <div style='height: 1000px;background:#EEE'>something for making scrollbars</div>
  <div id="myElement" style="position: absolute; bottom: 0px;">This
    stays at the top</div>
</body>
</html>

The code above is rendered as follows: