两种图片lazy加载的方式 第一个By JS中级交流群 成都-猎巫 第二个By 上海-zenki
// @description 准备为图片预加载使用的插件
// 使用的图片容器css类名为lazy-load-wrap
// 图片真实地址为data-lazy-src
// 当lazy-load-wrap容器进入视口,则开始替换容器内所有需要延迟加载的图片路径,并更改容器的加载状态
//第一种方法
$.fn.compassLazyLoad=function(){
var _HEIGHT=window.innerHeight,
_lazyLoadWrap=$(‘.lazy-load-wrap‘);
var methods={
setOffsetTop:function(){
$.each(_lazyLoadWrap,function(i,n){
$(n).attr({
‘top‘:n.offsetTop-_HEIGHT,
‘status‘:‘wait‘
});
})
},
isShow:function(){
var _scrollTop=$(window).scrollTop;
//利用image容器判断是否进入视口,而非image本身
$.each(_lazyLoadWrap,function(){
var _that=$(this);
if (_that.attr(‘status‘)===‘done‘) {
return;
};
if (_that.attr(‘top‘)<=_scrollTop) {
_that.find(‘img[data-lazy-src]‘).each(function(i,n){
n.src=$(n).data(‘lazy-src‘);
});
_that.attr(‘status‘,‘done‘);
};
})
},
scroll:function(){
$(window).on(‘scroll‘,function(){
methods.isShow();
});
},
init:function(){
methods.setOffsetTop();
methods.isShow();
methods.scroll();
}
};
methods.init();
}
//第二种方法
var exist=(function($){
var timer=null,
temp=[].slice.call($(‘.container‘));
ret={};
for(var i=0,len=temp.length-1;i<=len;i++){
ret[i]=temp[i];
}
var isExist=function(winTop,winEnd){
for(var i in ret){
console.log(ret);
var item=ret[i],
eleTop=item.offsetTop,
eleEnd=eleTop+item.offsetHeight;
if((eleTop>winTop&&eleTop<=winEnd)||(eleEnd>winTop&&eleEnd<=winEnd)){
$(item).css(‘background‘,‘none‘);
new Tab($(item).attr(‘id‘),data).init;
delete ret[i];
}
}
}
return {
timer:timer;
isExist:isExist;
};
})($);
//第三种方法
Zepto(function ($) {
var swiper = new Swiper(‘.swiper-container‘, {
pagination: ‘.swiper-pagination‘,
paginationClickable: true,
autoplay: 3000,
loop: true,
autoplayDisableOnInteraction: false
});
(function lazyLoad() {
var imgs = $(".lazyLoad");
var src = ‘‘;
$.each(imgs, function (index, item) {
src = $(item).attr(‘data-src‘);
$(item).attr(‘src‘, src);
});
})();
});
$(function () {
var lazyLoadTimerId = null;
/// 智能加载事件
$(window).bind("scroll", function () {
clearTimeout(lazyLoadTimerId);
lazyLoadTimerId = setTimeout(function () {
// 延迟加载所有图片
var isHttp = (location.protocol === "http:");
$("#ym_images img").each(function () {
var self = $(this);
if (self.filter(":above-the-fold").length > 0) {
var originUrl = self.attr("data-original");
self.attr("src", originUrl);
}
});
}, 500);
});
});
瀑布流无限加载实例
(function($){
$(function(){
var $container = $(‘.list-wrap-gd‘);
function layOutCallBack() {
$container.imagesLoaded(function(){
$container.masonry({
itemSelector: ‘.item-bar‘,
gutter: 10
});
});
$container.imagesLoaded().progress( function() {
$container.masonry(‘layout‘);
});
}
layOutCallBack();
$container.infinitescroll({
navSelector : "#more",
nextSelector : "#more a",
itemSelector : ".item-bar",
pixelsFromNavToBottom: 300,
loading:{
img: "/images/masonry_loading.gif",
msgText: ‘ ‘,
finishedMsg: "<em>已经到最后一页</em>",
finished: function(){
$("#more").remove();
$("#infscr-loading").hide();
}
},
errorCallback:function(){
$(window).unbind(‘.infscr‘);
},
pathParse: function (path, nextPage) {
var query = "";
var keyword=$("#search_keyword").val();
var cat_id=$("#cat_id").val();
var brand_id=$("#brand_id").val();
var country_id = $("#country_id").val();
query = query + "&namekeyword="+keyword;
query = query +"&cat_id="+cat_id
query = query + "&brand_id=" + brand_id;
query = query + "&country_id=" + country_id;
path = [path,query];
return path;
}
},
function(newElements) {
var $newElems = $( newElements ).css({ opacity: 0 });
$newElems.imagesLoaded(function(){
$newElems.animate({ opacity: 1 });
$container.masonry( ‘appended‘, $newElems, true );
layOutCallBack();
});
});
});
})(jQuery);
设置缓存,localStorage+cookie
var historys = { //缓存
isLocalStorage:window.localStorage?true:false,
set : function(key,value){ //设置缓存
if(this.isLocalStorage){
window.localStorage.setItem(key,value);
}else{
var expireDays = 365; //失效时间
var exDate=new Date();
exDate.setTime(exDate.getTime()+expireDays*24*60*60*1000);
document.cookie=key + "=" + escape(value)+";expires=" + exDate.toGMTString();
}
},
read : function(key){ //读取缓存
if(this.isLocalStorage){
return window.localStorage.getItem(key);
}else{
var arr,reg=new RegExp("(^| )"+key+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg)){
return unescape(arr[2]);
}else{
return null;
}
}
},
del : function(key){ //删除缓存
if(this.isLocalStorage){
localStorage.removeItem(key);
}else{
var exDate = new Date();
exDate.setTime(exDate.getTime() - 1);
var read_val=this.read(key);
if(read_val!=null) document.cookie= key + "="+read_val+";expires="+exDate.toGMTString();
}
}
}