网页图片预加载代码

function preLoadImage(images, cb) {
    // images: 图片路径数组; cb: 全部图片加载完的回调函数
  	var count = 0;
  	var allImageLoad = function() {
  		count += 1;
  		if (count >= images.length) {
			cb && cb();
  		};
  	};
  	images.forEach(function (d) {
  		var p = new Image();
  		p.onload = allImageLoad;
  		p.src = d;
  	});
};
preLoadImage(['images/p1.jpg', 'images/p2.png'], function () {
    // do something after all images loaded;
});

猜你喜欢

转载自blog.csdn.net/jdk137/article/details/84642606
今日推荐