forked from zhangmengxue/Fragment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplelazyload.html
More file actions
46 lines (43 loc) · 1.2 KB
/
simplelazyload.html
File metadata and controls
46 lines (43 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE HTML>
<style>
.A, .B {
border: 1px solid #CCC;
background: #EFEFEF;
margin-bottom: 60px;
line-height: 300px;
text-align: center;
font-size: 40px;
}
.A {
height: 1000px;
}
.B {
background: yellow;
height: 300px;
}
</style>
<div id="wrap">
<div class="A">撑高用的</div>
<div class="B">懒加载</div>
<div class="B">懒加载</div>
<div class="B">懒加载</div>
<div class="B">懒加载</div>
<div class="B">懒加载</div>
</div>
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script>
var $lazyModules = $(".B");
$(window).on("scroll", function(){
var sTop = $(this).scrollTop();
var wH = $(this).height();
$lazyModules.each(function(index, item){
var $item = $(item);
if(!$item.hasClass("loaded") && ($item.offset().top) < wH + sTop){
$item.addClass("loaded").hide().fadeIn(1000);
}
});
if($(".loaded").length == $lazyModules.length){
$(window).off("scroll");
}
}).trigger("scroll");
</script>