Fire function when div is scrolled to | jQuery
Is it possible that if a div is scrolled to, 开发者_如何学JAVAit should execute a function, like for example:
Yes? No?
$(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.
With https://www.npmjs.com/package/jquery-on-scrolled-to it's
$('.my-div').onScrolledTo(1,
function() {
alert('.my-div element became fully visible');
}
);
精彩评论