It's our wits that make us men.

图片预加载封装

Posted on By Wei Bin

HTML

<!DOCTYPE html>
<html>
<head>
	<title>图片预加载之无序加载</title>
	<meta charset="utf-8">
	<style type="text/css">
		*{padding: 0; margin:0;}
		html,body{
			height: 100%
		}
		a{text-decoration: none;}
		.box{
			text-align: :center;
		}
		.btn{
			display: inline-block;
			height: 30px;
			line-height: 30px;
			border: 1px solid #ccc;
			background-color: #fff;
			padding:0 10px;
			margin-right: 50px;
			color: #333;
		}
		.btn:hover{
			background-color: #eee
		}
		.loading{
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background-color: #eee;
			text-align: center;
			font-size: 30px;
		}
		.progress{
			margin-top: 300px;
		}
	</style>
</head>
<body>
<div class="box">
	<img id="img" src="http://i2.hoopchina.com.cn/user/308/15960308/13383588090.jpg" width="1200"></img>
	<p>
		<a href="javscript:;" class="btn" data-control='prev'>上一页</a>
		<a href="javscript:;" class="btn" data-control='next'>下一页</a>
	</p>

</div>
<div class="loading">
	<div class="progress">
		0%
	</div>
</div>
<script type="text/javascript" src="jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="preload.js"></script>
<script type="text/javascript">
	var imgs = [
		'http://i2.hoopchina.com.cn/user/308/15960308/13383588090.jpg',
		'http://img.dwstatic.com/ls/ls20170411_bg.jpg',
		'http://img.article.pchome.net/00/44/23/20/pic_lib/wm/2.jpg',
		'http://lcd.yesky.com/imagelist/2009/044/404q4y8g4m0p.jpg',
		'http://pc.duowan.com/uploads/allimg/2009-11/16101540-6-4k952.jpg',
		'http://lcd.yesky.com/imagelist/2009/044/cgro54wt2t2x.jpg'
	]

	
	//不用插件的写法
	/*var index = 0;
	var len = imgs.length;
	var count = 0;
	$progress = $('.progress')

	$.each(imgs,function(i,src){
		var imgObj = new Image();
		imgObj.src = src;
		//无论是否加载成功 都会继续循环,否则如果有一张加载不出来就没法进行了
		//下面是加载图片的回调 让load页走完
		$(imgObj).on('load error',function(){
			//每完成一次count++
			$progress.html(Math.round((count+1)/len * 100)+'%');
			if(count >= len -1){
				$('.loading').hide();
				document.title = '1/'+len;
			}
			count++;
		})
		
	})

	$('.btn').on('click',function(){
		if($(this).data('control') === 'prev'){
			//判断index 是否小于0 小于返回0 不小于返回index;Math.max返回较大的那个
			index = Math.max(0,--index)
		}
		else
		{
			index = Math.min(++index,len-1)
		}
		document.title = (index+1)+'/'+len;
		$("#img").attr('src',imgs[index])
	})*/


	//用插件的写法
	var index = 0;
	var len = imgs.length;	
	$progress = $('.progress')

	$.PreLoad(imgs,{
		all:"ordered",//有序加载
		each:function(count){ //每一张加载完执行的函数
			$progress.html(Math.round((count+1)/len * 100)+'%'); 
		},
		all:function(){ //所有图片加载完执行的函数
			$('.loading').hide();
			document.title = '1/'+len;
		}
	})
	//如果用不用JQURY插件调用 preload.js需要删除外边的大闭包 后面注释掉挂载到$.extend的方法
	/* $(function(){
	 	new PreLoad(imgs,{
			each:function(count){
				$progress.html(Math.round((count+1)/len * 100)+'%');
			},
			all:function(){
				$('.loading').hide();
				document.title = '1/'+len;
			}
		})

	 })*/

	 /*//有序加載,加載完第一張然後第二張,類似于漫畫	
	 var len = imgs.length,
	 	 count = 0;

	 orderLoad();	 
	 
	 function orderLoad(){
	 	var imgObj = new Image();
	 	$(imgObj).on("load error",function(){
	 		count++;
	 		if(count >= len){
	 			//所有图片加载完毕
	 		}else
	 		{
	 			orderLoad();
	 		}
	 	})
	 	imgObj.src = imgs[count]
	 }*/


	$('.btn').on('click',function(){
		if($(this).data('control') === 'prev'){
			//判断index 是否小于0 小于返回0 不小于返回index;Math.max返回较大的那个
			index = Math.max(0,--index)
		}
		else
		{
			index = Math.min(++index,len-1)
		}
		document.title = (index+1)+'/'+len;
		$("#img").attr('src',imgs[index])
	})
</script>
</body>
</html>

preload.js

//图片预加载
(function($){
	function PreLoad(imgs,options){
		//如果图片只有一张有可能是字符串传进来的
		this.imgs = (typeof imgs === 'string') ? [imgs]:imgs;
		this.opts = $.extend({},PreLoad.DEFAULTS,options)
		//判断是无序加载还是有序加载
		if(this.opts.order === "ordered"){
			this.ordered()
		}
		else{
			this._unoredered();
		}	

		
	}
	PreLoad.DEFAULTS = {
		order:"unoredered", //默认是无序的
		each:null, //每一张图片加载完毕后执行的方法
		all:null //所有图片加载完毕后执行的方法
	}
	//无序加载图片方法
	PreLoad.prototype._unoredered = function(){
		var imgs = this.imgs;
		var opts = this.opts;
		var count = 0;
		len = imgs.length;

		$.each(imgs,function(i,src){
			if(typeof src != "string") return ;
			var imgObj = new Image();
			imgObj.src = src;
			//无论是否加载成功 都会继续循环,否则如果有一张加载不出来就没法进行了
			//下面是加载图片的回调 让load页走完
			$(imgObj).on('load error',function(){
				opts.each && opts.each(count);
				
				if(count >= len -1){					
					opts.all && opts.all();
				}
				count++;
			})		
		})
	}

	PreLoad.prototype.ordered = function(){
		var imgs = this.imgs;
		var opts = this.opts;
		var len = imgs.length;
		var count = 0;
		orderLoad();	 
	 
		 function orderLoad(){
		 	var imgObj = new Image();
		 	$(imgObj).on("load error",function(){
		 		opts.each && opts.each(count)

		 		if(count >= len){
		 			//所有图片加载完毕
		 			opts.all && opts.all();
		 		}else
		 		{
		 			orderLoad();
		 		}
		 		count++;
		 	})
		 	imgObj.src = imgs[count]
		 }
	}

	//转换成jquery插件写法
	$.extend({
		PreLoad:function(imgs,opts){
			new PreLoad(imgs,opts)
		}
	})

})(jQuery)