document.onscroll=function()
{
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
var cHeight=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight;
var op=document.getElementById('showIt');
if(scrollTop>(op.offsetTop-cHeight))
alert('触发了')
}
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
</style>
</head>
<body style="height:2000px;">
<p id="showIt"></p>
</body>
<script type="text/javascript">
</script>
</html>
先上答案。
废话不多说,解释下逻辑。
当你滚动窗口的时候,只有你的scrollTop是变化的,当你滚动的越来越多到了一定程度,那就能看到你的p了。
知道这个道理后就很简单了。
其实就是只要你的滚动距离大于p当前的TOP减去视窗大小,你就能看到这个p了。
所以条件按照这个写就可以。了
用jQuery 的话可以查查position,offset这两个API用法,用别的东西,那就实现这两个API。