jquery判断某个div是否进入可视区域
发布时间:2015-10-14, 16:03:36 分类:HTML | 编辑 off 网址 | 辅助
正文 2609字数 328,574阅读
根据div的top和滚动条top判断,参照lazyload做了个demo如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript" src="/js/jquery-1.4.4.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
<!--//
$(document).ready(function(){
$(window).bind("scroll", function(event){
$("div").each(function(){
var fold = $(window).height() + $(window).scrollTop();
if( fold <= $(this).offset().top){
$(this).trigger("appear");
}
});
});
$("div").each(function(){
if( $(window).height() > $(this).offset().top){
$(this).html("出现在可视区域");
}
$(this).one("appear",function(){
$(this).html("出现在可视区域");
});
});
});
//-->
</script>
</head>
<body>
<div>111</div>
<div>1111</div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div showed = "N">111111111111</div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div showed = "N">111111111111</div>
</body>
</html>
Run code
Cut to clipboard
我把jquery.lazyload.js给改了,做成了一个我要的效果的
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 1 条评论 »
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>js</title> <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $(window).scroll(function () { var a = document.getElementById("eq").offsetTop; if (a >= $(window).scrollTop() && a < ($(window).scrollTop()+$(window).height())) { alert("div在可视范围"); } }); }); </script> </head> <body> <div style="width:1px;height:2000px;"></div> <div id="eq" style=" width:100px; height:100px; background-color:Red;">1</div> <div style="width:1px;height:2000px;"></div> </body> </html>