gpt4 book ai didi

jQuery:当 div 进入 View 时如何触发事件?

转载 作者:行者123 更新时间:2023-12-03 21:33:46 24 4
gpt4 key购买 nike

当页面滚动到足以让 div 进入 View 时,有什么方法可以用 jQuery 触发事件吗?

最佳答案

如果您只需要触发一次:

$(window).scroll(function(){
// This is then function used to detect if the element is scrolled into view
function elementScrolled(elem)
{
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
return ((elemTop <= docViewBottom) && (elemTop >= docViewTop));
}

// This is where we use the function to detect if ".box2" is scrolled into view, and when it is add the class ".animated" to the <p> child element
if(elementScrolled('. box2')) {


// Your function here

}
});

完整答案:https://www.thewebtaylor.com/articles/how-to-detect-if-an-element-is-scrolled-into-view-using-jquery

关于jQuery:当 div 进入 View 时如何触发事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4837772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com