jQuery: Run when scroll over a div of a certain class

馋奶兔 提交于 2020-01-06 04:15:32

问题


so I have multiple divs of the same class below each other. I now want to trigger a "loading comments"-function every time a visitor scrolls to a new div. However, I have no idea how to track jQuery scroll over a div. The comments for each div should only be loaded once, my idea would be to save the current position in a variable and only load comments when the new scroll position is greater than the old one.

Can you please help me out?

EDIT: I created a image that shows what I need to do. Image: http://i.imgur.com/78EYK.jpg

EDIT 2: SOLVED! Solved with Viewport (thanks to Royi Namir) http://www.appelsiini.net/projects/viewport

My code:

load = $(".my-div:in-viewport").attr('id');
load_comments(load);

This is executed every second.


回答1:


you should read about viewport

Viewport ads couple of extra selectors to jQuery. With these selectors you can check whether element is inside or outside of viewport. To see how it works check the demo.

http://www.appelsiini.net/projects/viewport/3x2.html



来源:https://stackoverflow.com/questions/8554580/jquery-run-when-scroll-over-a-div-of-a-certain-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!