3

I want to be able to find out when a page element is on or off the viewers screen. Specifically, I'd like to be able to "turn off" an html 5 canvas (Processing.js) script if the user has scrolled past the canvas and back on when it is visible again in the users browser.

Does this make sense? I am thinking that there must be some way for the DOM to fire off a notification via ajax to turn on and off the canvas script.

thanks.

/////////// EDIT (final code) ////////// Here is the final code that I used:

$(window).bind('scroll', function(){
   var $canvas = $('#canvas');
   var pos     = $canvas.offset();
   var total = pos.top + $($canvas).height();
   var wndtop  = $(this).scrollTop();

   if(wndtop <= pos || wndtop >= total ){ 

   }

});
superUntitled
  • 22,351
  • 30
  • 83
  • 110
  • Try http://www.appelsiini.net/projects/viewport , it lets you select elements depending if they are currently viewable you could check it on every scroll event? – Adam Jul 15 '10 at 20:10

3 Answers3

3

You can attach an event handler to the window scrolling. Like

$(window).bind('scroll', function(){
   var $canvas = $('#hlogo');
   var pos     = $canvas.offset();
   var wndtop  = $(this).scrollTop();
   var wndleft = $(this).scrollLeft();

   if(wndtop <= pos.top && wndleft <= pos.left)
      $canvas.hide();
});
jAndy
  • 231,737
  • 57
  • 305
  • 359
0

You can do that by using DOM properties element.y, element.style,height and window.scrollY.

srigi
  • 1,682
  • 1
  • 15
  • 30
0

You should be able to adjust the code in the link below to your needs. Check if element is visible after scrolling

Community
  • 1
  • 1
Fabian
  • 13,603
  • 6
  • 31
  • 53