这是我的最后一个代码。在此代码中,延迟加载不起作用,所有图像都立即下载。
js:
$(window).load(function(){
$('.grid').imagesLoaded(function(){
$('.grid').isotope({
itemSelector: '.grid-item',
containerClass: 'isotope',
layoutMode: 'masonry',
percentPosition: true
});
});
});
$(window).scroll(function(){
$('.grid').isotope({
itemSelector: '.grid-item',
containerClass: 'isotope',
layoutMode: 'masonry',
percentPosition: true
});
});
$(document).ready(function(){
$('.grid-item img').lazyload({
effect: 'fadeIn',
threshold : 300
});
});
css和html:
.grid {
max-width: 69em;
list-style: none;
margin: 20px auto;
padding: 0;
}
.grid::after {
content: "";
clear: both;
display: table;
}
.grid-sizer,
.grid-item {
display: block;
padding: 7px;
}
@media (min-width: 530px) {
.grid-sizer,
.grid-item {
float: right;
width: 50%;
}
}
@media (min-width: 960px) {
.grid-sizer,
.grid-item {
width: 33%;
}
}
<div class="grid">
<div class="grid-sizer"></div>
<div class="grid-item">
<img src="assets/images/gallery/01.JPG"/>
</div>
<div class="grid-item">
<img src="assets/images/gallery/02.JPG"/>
</div>
<div class="grid-item">
<img src="assets/images/gallery/03.JPG"/>
</div>
<div class="grid-item">
<img src="assets/images/gallery/04.JPG"/>
</div>
<div class="grid-item">
<img src="assets/images/gallery/05.JPG"/>
</div>
<div class="grid-item">
<img src="assets/images/gallery/06.JPG"/>
</div>
<div class="grid-item">
<img src="assets/images/gallery/07.JPG"/>
</div>
</div>