jquery 怎么判断 某个div 是否进入可视区域,或者是滚动条拖拽到某个位置时,出发某个事件!
蝶舞and飘雪我就是想像lazyload那样,lazyloading是滚动条到某个位置时显示图片,将图片路径放入src属性中,我想要的效果就是,滚动条拖动时,某个div进...
蝶舞and飘雪
我就是想像lazyload那样,lazyloading是滚动条到某个位置时显示图片,将图片路径放入src属性中,我想要的效果就是,滚动条拖动时,某个div进入浏览器可视区域,之后触发某个事件。 展开
我就是想像lazyload那样,lazyloading是滚动条到某个位置时显示图片,将图片路径放入src属性中,我想要的效果就是,滚动条拖动时,某个div进入浏览器可视区域,之后触发某个事件。 展开
3个回答
展开全部
根据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>
<!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>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询