Posted By

marcio on 02/21/10


Tagged

scroll jquery


Versions (?)

change div based on how far down the page you have scrolled


 / Published in: jQuery
 

URL: http://stackoverflow.com/questions/625143/change-div-based-on-how-far-down-the-page-you-have-scrolled

  1. $(document).ready(function() {
  2. function isScrolledIntoView(elem) {
  3. var docViewTop = $(window).scrollTop();
  4. var docViewBottom = docViewTop + $(window).height();
  5.  
  6. var elemTop = $(elem).offset().top;
  7. var elemBottom = elemTop + $(elem).height();
  8.  
  9. return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
  10. }
  11.  
  12. var myelement = $('#formcontainer'); // the element to act on if viewable
  13. $(window).scroll(function() {
  14. if(isScrolledIntoView(myelement)) {
  15. // do something when element is scrolled to and viewable
  16. } else {
  17. // do something when element is not viewable
  18. }
  19. });
  20. });

Report this snippet  

You need to login to post a comment.