预加载
var img=new Image();
img.addEventListener("load",loadHandler);
img.src="./img/3-.jpg";
document.body.appendChild(img);
console.log(img.offsetWidth);
function loadHandler(e){
console.log(img.width);
}
for(var i=3;i<80;i++){
let img=new Image();
img.addEventListener("load",loadHandler);
img.src=`./img/${i}-.jpg`;
}
function loadHandler(e){
console.log(e.currentTarget.width,e.currentTarget.src);
}
var num=3;
var arr=[];
document.addEventListener("startLoad",loadImage);
loadImage();
function loadImage(){
var img=new Image();
img.addEventListener("load",loadHandler);
img.src=`./img/${num}-.jpg`;
}
function loadHandler(e){
arr.push(e.currentTarget);
console.log(e.currentTarget.width,e.currentTarget.src);
message();
}
function message(){
num++;
if(num>79){
return;
}
var evt=new Event("startLoad");
document.dispatchEvent(evt);
}
var num=3;
var arr=[];
loadImage();
function loadImage(){
var img=new Image();
img.addEventListener("load",loadHandler);
img.src=`./img/${num}-.jpg`;
}
function loadHandler(e){
arr.push(e.currentTarget);
num++;
if(num>79){
arr.forEach(function(item){
console.log(item.width,item.src);
});
return;
}
loadImage();
}
var arr=[];
var num=3;
loadImage();
function loadImage(){
var img=new Image;
img.addEventListener("load",loadHandler);
img.src=`./img/3-.jpg`;
}
function loadHandler(e){
arr.push(e.currentTarget.cloneNode(false));
console.log(e.currentTarget.width,e.currentTarget.src);
num++;
if(num>79){
init(arr);
return;
}
e.currentTarget.src=`./img/${num}-.jpg`;
}
function init(){
}
图片复制
var img=new Image();
img.src="./img/4.jpg";
document.body.appendChild(img);
var img1=img.cloneNode(false);
document.body.appendChild(img1);
img.src="./img/5.jpg";
var img2=img.cloneNode(false);
document.body.appendChild(img);
img.src="./img/6.jpg";