开发者

Fire function when div is scrolled to | jQuery

开发者 https://www.devze.com 2023-01-20 23:05 出处:网络
Is it possible that if a div is scrolled to, 开发者_如何学JAVAit should execute a function, like for example:

Is it possible that if a div is scrolled to, 开发者_如何学JAVAit should execute a function, like for example:

Fire function when div is scrolled to | jQuery

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');
    }
);
0

精彩评论

暂无评论...
验证码 换一张
取 消