jquery.imgpreload.min.js插件实现页面图片预加载

页面分享地址:

http://wenku.baidu.com/link?url=_-G8miwbgDmEj6miyFtjit1duJggBCJmFjR2jky_G1VftD9eS9kwGOlFWAORk2_fotM4RbzRvtcaiQu2y_BU3Gpj0rYRy_EWrgXEuFeja0q

页面分享地址:http://www.jb51.net/article/21987.htm

Javascript

Jquery

实现页面图片预加载百分比展现

如果需要在页面初始加载时显示加载进度。主要是指图片很多的情况下:

可以使用第三方

Jquery

插件

 

jquery.imgpreload.min.js

调用里面的方法:

imgpreload

即可,实例如下:

varimgNum = 0;

var images = [];

$(function(){ preloadImg(); });

//

里面有两种方式

functionpreLoadImg() {

//

第一种方式:通过

dom

方法获取页面中的所有

img

,包括

<img>

标签和

css

中的

background-image

/*get all imgs those tag is <img>

varimgs = document.images;

for (vari = 0; i<imgs.length; i++) {

images.push(imgs[i].src);

}

//get all images in style

varcssImages = getallBgimages();

for (var j = 0; j <cssImages.length; j++) {

images.push(cssImages[j]);

}*/

//

第二种方式:把所有该网页上用到的图片文件都预先放入一个数组里

$.imgpreload(['images/bg1.jpg', 'images/bg2.jpg'], function () {

//

此处是显示进度百分比时需要用到的背景图,这个可以先加载进去

});

//then push all other images in array to load

images.push("images/test_1.png");

images.push("images/test_2.png");

images.push("images/test_3.png");

//

。。。

images.push("images/test_n.png");

/*

这里是真正的图片预加载

preload*/

$.imgpreload(images,

{

each: function () {

/*this will be called after each image loaded*/

var status = $(this).data('loaded') ? 'success' : 'error';

if (status == "success") {

var v = (parseFloat(++imgNum) / images.length).toFixed(2);

$("#percentShow").html(Math.round(v * 100) +

"<sup>%</sup>");

}

},

all: function () {

/*this will be called after all images loaded*/

$("#percentShow ").html("100<sup>%</sup>");

$("percentShow").fadeOut(1000);

$(".main").show();

}

});

}

//get all images in style

(此方法引用其他博客的)

functiongetallBgimages() {

varurl, B = [], A = document.getElementsByTagName('*');

A = B.slice.call(A, 0, A.length);

while (A.length) {

url = document.deepCss(A.shift(), 'background-image');

if (url) url = /url\(['"]?([^")]+)/.exec(url) || [];

url = url[1];

if (url&&B.indexOf(url) == -1) B[B.length] = url;

}

return B;

}

document.deepCss = function (who, css) {

if (!who || !who.style) return '';

var sty = css.replace(/\-([a-z])/g, function (a, b) {

returnb.toUpperCase();

});

if (who.currentStyle) {

returnwho.style[sty] || who.currentStyle[sty] || '';

}

var dv = document.defaultView || window;

returnwho.style[sty] ||

dv.getComputedStyle(who, "").getPropertyValue(css) || '';

}

Array.prototype.indexOf = Array.prototype.indexOf ||

function (what, index) {

index = index || 0;

var L = this.length;

while (index < L) {

if (this[index] === what) return index;

++index;

}

return -1;

}

这样就能在页面图片很多,且网速很慢的情况下给予用户一个百分比提示。

在做这个之前,由于每次本地测试加载都很快,

百分比瞬间到

100%

然后消失,

为了看上去

有那么一回事,我还写了一个伪百分比进度条,仅供玩耍:

var t = window.setTimeout("preLoad()", 100);

functionpreLoad() {

$("#loading div").animate({ width: step + "px" }, 50).text(step + "%");

step += 1;

if (step <= 100) {

t = window.setTimeout("preLoad()", 100);

} else {

clearTimeout(t);

$("#loading").fadeOut(1000);

$("#preloadImg").fadeOut(1000);

$(".main").show();

}

(

这是一个页面初始化完成之后,在页面上有一个模拟百分比不断增长的过程,到

100%

消失进度条,

显示主页面,

不过跟实际页面加载没任何关系,

可以忽悠不懂且有想要此功能

的人,哈哈

)

上一篇:hive1.2.1实战操作电影大数据!


下一篇:kindle paperwhite2 root 密码修改方法