javascript画像のプリロード
11820 ワード
<!DOCTYPE >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title> </title>
</head>
<body>
<img id="song" width="300" src="http://www.baidu.com/img/bdlogo.gif" _src="http://p2009c.zbjimg.com/task/2009-12/03/188273/middlexznxwkia.gif" />
</body>
</html>
<script type="text/javascript">
//
var defurl = 'http://www.baidu.com/img/bdlogo.gif';
//
var imgurl = 'http://www.planeart.cn/demo/imgReady/vistas24.jpg';
// gif
var gifurl = 'http://p2009c.zbjimg.com/task/2009-12/03/188273/middlexznxwkia.gif';
//
function loadImage(url, callback) {
var img = new Image(); // Image ,
img.onload = function(){
img.onload = null;//gif ie
callback(img);
}
img.src = url;
}
var img = document.getElementById('song');
var url = img.getAttribute('_src')
loadImage(gifurl,function(){
//alert('ok');
img.src = url;
})
/**
*
* @param url
* @param callback
*/
var imgLoad = function (url, callback) {
var img = new Image();
img.src = url;
if (img.complete) {
callback(img.width, img.height);
} else {
img.onload = function () {
callback(img.width, img.height);
img.onload = null;
};
};
};
// :
// 05.27: 1、 :error > ready > load;2、 this img
// 04-02: 1、 2、
/**
* -
*
* : , , ready
* w3c complete,ie onload,
*
* @version 2011.05.27
* @author TangBin
* @see http://www.planeart.cn/?p=1121
* @param {String}
* @param {Function}
* @param {Function} ( )
* @param {Function} ( )
* @example imgReady('http://www.google.com.hk/intl/zh-CN/images/logo_cn.png', function () {
alert('size ready: width=' + this.width + '; height=' + this.height);
});
*/
var imgReady = (function () {
var list = [], intervalId = null,
//
tick = function () {
var i = 0;
for (; i < list.length; i++) {
list[i].end ? list.splice(i--, 1) : list[i]();
}
;
!list.length && stop();
},
//
stop = function () {
clearInterval(intervalId);
intervalId = null;
};
return function (url, ready, load, error) {
var onready, width, height, newWidth, newHeight,
img = new Image();
img.src = url;
// ,
if (img.complete) {
ready.call(img);
load && load.call(img);
return;
}
;
width = img.width;
height = img.height;
//
img.onerror = function () {
error && error.call(img);
onready.end = true;
img = img.onload = img.onerror = null;
};
//
onready = function () {
newWidth = img.width;
newHeight = img.height;
if (newWidth !== width || newHeight !== height ||
//
newWidth * newHeight > 1024
) {
ready.call(img);
onready.end = true;
}
;
};
onready();
//
img.onload = function () {
// onload onready
// onready
!onready.end && onready();
load && load.call(img);
// IE gif onload, onload
img = img.onload = img.onerror = null;
};
//
if (!onready.end) {
list.push(onready);
// ,
if (intervalId === null) intervalId = setInterval(tick, 40);
}
;
};
})();
var img_url = 'http://www.planeart.cn/demo/imgReady/vistas24.jpg';
imgReady(img_url, function () {
//console.info(this.width);
//console.info(this.height);
alert(this.width + '
' + this.height);
document.getElementById('song').src = img_url;
})
</script>