简单版的预加载
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Page Title</title>
<script src="../jquery.1.10.js"></script>
</head>
<style>
.loading{
position: fixed;top: 0;
left:0;
width: 100%;
height: 100%;
background-color: #eee;
text-align: center;
font-size: 30px;
}
.progress{
margin-top: 300px;
}
.btn.hover{
background-color: #eee;
}
</style>
<body>
<div class="box">
<img src="http://preview.quanjing.com/top034/top-924268.jpg" alt="pic" id="img" height="500">
<p>
<a href="#" class="btn" data-control="prev">上一页</a>
<a href="#" class="btn" data-control="next">下一页</a>
</p>
</div>
<div class="loading">
<p class="progress">0%</p>
</div>
</body>
<script>
var imgs = [
"http://preview.quanjing.com/top034/top-924268.jpg",
"http://preview.quanjing.com/CPS1/152-0111.jpg",
"http://preview.quanjing.com/bld011/bld125797.jpg",
"http://preview.quanjing.com/east025/east-ep-a41-8167880.jpg"
]
var index = 0,
len = imgs.length,
count = 0,
$progress = $('.progress');
$('.btn').on('click',function (params) {
if('prev' === $(this).data('control')){
index = Math.max(0,--index);
}else{
index = Math.min(len -1, ++index)
}
document.title = (index + 1) + '/' +len;
$('#img').attr('src',imgs[index]);
})
$.each(imgs, function (i, src) {
var imgObj = new Image();
$(imgObj).on('load error', function () {
$progress.html(Math.round((count + 1) / len * 100) + "%");
console.log("$progress ", (count + 1) / len * 100);
if (count >= len - 1) {
$('.loading').hide();
document.title = '1/' + len;
}
count++
});
imgObj.src = src;
})
</script>
</html>
封装成jquery插件
//---------preload.js
(function($){
function preload(imgs, options) {
this.imgs = (typeof imgs === 'string') ? [imgs] : imgs;
this.opts = $.extend({}, preload.defaults, options);
this._unoredered() //加下划线说明方法是私有
}
preload.defaults = {
each: null,//所有图片加载完成后执行
all: null//所有图片加载完毕后执行
};
preload.prototype._unoredered = function(){ //无序加载
var img = this.imgs,
opts = this.opts,
count = 0,
len = imgs.length;
$.each(imgs, function (i, src) {
debugger;
if(typeof src != 'string') return;
var imgObj = new Image();
$(imgObj).on('load error', function () {
opts.each && opts.each(count);//判断 each方法是否存在
if (count >= len - 1) {
opts.all && opts.all();
}
count++
});
imgObj.src = src;
})
}
$.extend({
preload:function(imgs,opts){
new preload(imgs,opts)
}
})
})(jQuery)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Page Title</title>
<script src="../jquery.1.10.js"></script>
</head>
<style>
.loading{
position: fixed;top: 0;
left:0;
width: 100%;
height: 100%;
background-color: #eee;
text-align: center;
font-size: 30px;
}
.progress{
margin-top: 300px;
}
.btn.hover{
background-color: #eee;
}
</style>
<body>
<div class="box">
<img src="http://preview.quanjing.com/top034/top-924268.jpg" alt="pic" id="img" height="500">
<p>
<a href="#" class="btn" data-control="prev">上一页</a>
<a href="#" class="btn" data-control="next">下一页</a>
</p>
</div>
<div class="loading">
<p class="progress">0%</p>
</div>
</body>
<script src="./preload.js"></script>
<script>
var imgs = [
"http://preview.quanjing.com/top034/top-924268.jpg",
"http://preview.quanjing.com/CPS1/152-0111.jpg",
"http://preview.quanjing.com/bld011/bld125797.jpg",
"http://preview.quanjing.com/east025/east-ep-a41-8167880.jpg"
],
$progress = $('.progress'),
len = imgs.length;
$.preload(imgs,{
each:function(count){
console.log(count);
$progress.html(Math.round((count + 1) / len * 100) + "%");
},
all:function(){
$('.loading').hide();
document.title = '1/' + len;
}
})
var index = 0;
$('.btn').on('click',function (params) {
if('prev' === $(this).data('control')){
index = Math.max(0,--index);
}else{
index = Math.min(len -1, ++index)
}
document.title = (index + 1) + '/' +len;
$('#img').attr('src',imgs[index]);
})
</script>
</html>