#showIt{width: 200px;height: 200px;background-color: red;position: absolute;top: 1500px;}
document.οnscrοll=function()
{
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
var cHeight=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight;
var oDiv=document.getElementById('showIt');
if(scrollTop>(oDiv.offsetTop-cHeight))
alert('触发了')
}
先上答案。
废话不多说,解释下逻辑。
当你滚动窗口的时候,只有你的scrollTop是变化的,当你滚动的越来越多到了一定程度,那就能看到你的DIV了。
知道这个道理后就很简单了。
其实就是只要你的滚动距离大于DIV当前的TOP减去视窗大小,你就能看到这个DIV了。
所以条件按照这个写就可以。了