向下滚动时,如何在元素进入窗口 View 时立即对元素执行函数?
最佳答案
我不妨把它贴在这里作为答案:
function elementInView($elem, vps, vpe) {
var elempos = $elem.position();
var pagestart = elempos.top + vps;
var pageend = elempos.top + vps + $elem.height();
var offset = $elem.height() - Math.max(0,vps-pagestart) - Math.max(0,pageend-vpe);
return (vpe > 0 && offset > -200);
}
$('#container').bind('scroll', function() {
var $container = $(this);
var vps = $container.scrollTop();
var vpe = vps + $container.height();
$('li', '#container').each(function() {
var $this = $(this);
if (elementInView($this,vps,vpe)) {
// $this is now in view
}
});
});
我意识到这有一些性能问题,可以优化,但这应该是一个开始。
关于javascript - 当元素进入窗口的 View 时执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2495615/