$.preloadImages = function () {
for (var i = 0; i < arguments.length; i++) {
$('img').attr('src', arguments[i]);
}
};
$.preloadImages('img/hover-on.png', 'img/hover-off.png');
$('img[data]').load(function(){
var __this__ = $(this);
var url = __this__.attr('data');
var src = __this__.attr('src');
if(url ==''|| url == src)//这里判断如果图片实际地址不存在或者已经加载不处理
{
return;
}
var img =newImage();//实例化一个图片的对象
img.src = url;//将要显示的图片加载进来
if(img.complete)//如果图片已经加载存在浏览器缓存中直接处理
{
__this__.attr('src',url);//将要显示的图片替换过来
return;
}
img.onload =function(){//要显示的图片加载完成后做处理
__this__.attr('src',url);
}
});
var imgNum = 0;
var images = [];
$(function(){ preloadImg(); });
//里面有两种方式
function preLoadImg() {
//第一种方式:通过dom方法获取页面中的所有img,包括<img>标签和css中的background-image
/*get all imgs those tag is <img>
var imgs = document.images;
for (var i = 0; i < imgs.length; i++) {
images.push(imgs[i].src);
}
//get all images in style
var cssImages = 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(此方法引用其他博客的)
function getallBgimages() {
var url, 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) {
return b.toUpperCase();
});
if (who.currentStyle) {
return who.style[sty] || who.currentStyle[sty] || '';
}
var dv = document.defaultView || window;
return who.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;
}
var t = window.setTimeout("preLoad()", 100);
function preLoad() {
$("#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();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有