views:

50

answers:

1

Is it possible that if a div is scrolled to, it should execute a function, like for example:

alt text

Yes? No?

+1  A: 
$(window).scroll(function () { 
  if ($(window).scrollTop() <= $("div#14").position().top && $(window).scrollTop() + $(window).height() >= $("div#14").position().top) {
    if ($("div#14").position().top + $("div#14").height() <= $(window).scrollTop() + $(window).height()) {
      do_something();
    }
  }
});

EDIT Sorry, I put the ">=" and "<=" wrong.

Watch it here: http://jsfiddle.net/cBdhk/4/

EDIT 2 I updated the link but forgot updating the code.

Diego
You have a typo: `$(this).heigth()` but it doesn't seem to work though. Check out this demo: http://jsfiddle.net/PjLPq/ (note: use Firebug to look into the console log).
YouBook
Thanks @YouBook. It is already fixed.
Diego
Woah, outstanding! Thanks.
YouBook