关于JS中图片预加载问题 <span id=myimg><img name=img1 src=1.gif onclick="myimg.innerHTML='<img name=img1 src=2.gif>'"></span> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没有用呀,因为2.GIF还没下载完成。必须要等待下载完毕后,再次刷新再有效。 <img name=img1 src=1.gif onclick="this.src=2.gif" width=n height=n>固定 试试在bady里onLoad预先加载2.gif ---------------//声明一个数组,把图片地址预存进去var imgname=new Array()imgname[0]="1.jpg"imgname[1]="2.jpg"//预读图片for (i=0;i<=imgname.length-1;i++) { imgpreload[i]=new Image() imgpreload[i].src=imgname[i]} xjdawu 这招,我试过了.不行,当二个图片大小相差很大时,还是老样子.显示出第一幅图片的大小.有什么什么办法,判断,或等待图片加载完毕.继续求.... new image() 用这个方法.....<html><head><script language="javascript">function preload(imgObj,imgSrc){ eval(imgObj+'=new Image()'); eval(imgObj+'.src="'+imgSrc+'"'); }function ini(){ //preload('imageA','14.jpg'); //preload('imageB','15.jpg'); //preload('imageC','16.jpg'); // preload('imageD','15.jpg'); //preload('imageE','14.jpg'); //preload('imageF','15.jpg'); //preload('imageG','14.jpg'); //preload('imageH','15.jpg'); //preload('imageI','14.jpg'); //preload('imageJ','15.jpg'); //preload('imageK','14.jpg'); //preload('imageL','15.jpg'); var theimage=new Array("14.jpg","15.jpg","16.jpg","2.jpg","8.jpg","11.jpg","22.jpg","23.jpg","33.jpg","35.jpg"); var the_img=document.getElementById("img1"); // the_img.src=theimage[9]; var thenum=Math.random(); thenum=thenum*10; thenum=Math.floor(thenum); the_img.src=theimage[thenum]; //getimgid(); } function getimgid(){ var the_img=document.getElementById("img1"); the_img.src=(the_img.src==imageA.src)?imageB.src:imageA.src; // the_img.style.filter=alpha(opacity=90); window.setTimeout("getimgid()",3000); // the_img.src=imageA.src; } function img2small(){ } </script></head><body onload='ini()'><img src='14.jpg' id='img1' width='200' style='filter:alpha(opacity=30)'/></body></html> new image() 用这个方法实现加载.....<html><head><script language="javascript">function preload(imgObj,imgSrc){ eval(imgObj+'=new Image()'); eval(imgObj+'.src="'+imgSrc+'"'); }function ini(){ //preload('imageA','14.jpg'); //preload('imageB','15.jpg'); //preload('imageC','16.jpg'); // preload('imageD','15.jpg'); //preload('imageE','14.jpg'); //preload('imageF','15.jpg'); //preload('imageG','14.jpg'); //preload('imageH','15.jpg'); //preload('imageI','14.jpg'); //preload('imageJ','15.jpg'); //preload('imageK','14.jpg'); //preload('imageL','15.jpg'); var theimage=new Array("14.jpg","15.jpg","16.jpg","2.jpg","8.jpg","11.jpg","22.jpg","23.jpg","33.jpg","35.jpg"); var the_img=document.getElementById("img1"); // the_img.src=theimage[9]; var thenum=Math.random(); thenum=thenum*10; thenum=Math.floor(thenum); the_img.src=theimage[thenum]; //getimgid(); } function getimgid(){ var the_img=document.getElementById("img1"); the_img.src=(the_img.src==imageA.src)?imageB.src:imageA.src; // the_img.style.filter=alpha(opacity=90); window.setTimeout("getimgid()",3000); // the_img.src=imageA.src; } function img2small(){ } </script></head><body onload='ini()'><img src='14.jpg' id='img1' width='200' style='filter:alpha(opacity=30)'/></body></html> re 白夜花寒<img name=img1 src=1.gif onclick="this.src=2.gif">当onclick的时候, img1 的 src 立即就变为了 2.gifgetimgid() 等于无效而2.gif的加载在eval(imgObj+'=new Image()');eval(imgObj+'.src="'+imgSrc+'"'); 等效于imgpreload[i]=new Image()imgpreload[i].src=imgname[i]这个脚本,应该是翻转脚本不知道你有没有试过,如果第一幅是10*10 jpg 图第二幅是 640*480 GIF 30帧动画 你翻转后,取宽度高度试一试.我试过 complete 属性,可还是不行.complete 在第一次装载完毕时,就被置为true了.?????? 用timer 等待没错,可是用什么判断第二幅图已经显示上了呢?tks继续求..... 如果加载第二幅图片时足够慢的话,确实有这种现象------------------------<script>function getWidth(objName) { if(eval(objName+".readyState") != 'complete'){ setTimeout("getWidth('"+objName+"')",10) return } return eval(objName+".width")}</script><img name=img1 src=image/163.gif onclick="this.src='http://www.csdn.net/images/biaoshi.gif';alert(getWidth(this.name))"> xjdawu(左右互搏)问题解决了,呵呵,要稍为改动一下,感谢。感谢大家。<script>function getWidth(objName) { if(eval(objName+".readyState") != 'complete'){ setTimeout("getWidth('"+objName+"')",10) }else { alert( eval(objName+".width") ); }}</script><img name=img1 src=163.gif onclick="this.src='http://www.online99.net/pic_rf/rf_show3.JPG';getWidth(this.name)"> 【原创】《植物大战僵尸》网页脚本版,持续更新,敬请期待... Javascript里的单引号双引号怎么样用 如何循环读取div中包含的其他的div jsp页面表格如何求某几行的和 为何我的菜单会跑位,急!!! javascript里面把一个汉字转成两个byte值,有没有人知道 xml数据岛绑定的问题 关于赋值的问题,请教各位高手 有什么容器既有innerHTML属性,又有滚动条的. A页面中包含IFRAME页面为B,A提交到C.ASP,在C中如何读取B中的TEXT等字段??? 一个获取图片坐标值的问题(能着请进) 如何格式化数字?
因为2.GIF还没下载完成。必须要等待下载完毕后,再次刷新再有效。
//声明一个数组,把图片地址预存进去
var imgname=new Array()
imgname[0]="1.jpg"
imgname[1]="2.jpg"//预读图片
for (i=0;i<=imgname.length-1;i++) {
imgpreload[i]=new Image()
imgpreload[i].src=imgname[i]
}
不行,当二个图片大小相差很大时,还是老样子.
显示出第一幅图片的大小.有什么什么办法,判断,或等待图片加载完毕.继续求....
<head>
<script language="javascript">
function preload(imgObj,imgSrc){
eval(imgObj+'=new Image()');
eval(imgObj+'.src="'+imgSrc+'"');
}function ini(){
//preload('imageA','14.jpg');
//preload('imageB','15.jpg');
//preload('imageC','16.jpg');
// preload('imageD','15.jpg');
//preload('imageE','14.jpg');
//preload('imageF','15.jpg');
//preload('imageG','14.jpg');
//preload('imageH','15.jpg');
//preload('imageI','14.jpg');
//preload('imageJ','15.jpg');
//preload('imageK','14.jpg');
//preload('imageL','15.jpg');
var theimage=new Array("14.jpg","15.jpg","16.jpg","2.jpg","8.jpg","11.jpg","22.jpg","23.jpg","33.jpg","35.jpg");
var the_img=document.getElementById("img1");
// the_img.src=theimage[9];
var thenum=Math.random();
thenum=thenum*10;
thenum=Math.floor(thenum);
the_img.src=theimage[thenum];
//getimgid();
} function getimgid(){
var the_img=document.getElementById("img1");
the_img.src=(the_img.src==imageA.src)?imageB.src:imageA.src;
// the_img.style.filter=alpha(opacity=90);
window.setTimeout("getimgid()",3000);
// the_img.src=imageA.src;
} function img2small(){
}
</script>
</head>
<body onload='ini()'>
<img src='14.jpg' id='img1' width='200' style='filter:alpha(opacity=30)'/></body>
</html>
<head>
<script language="javascript">
function preload(imgObj,imgSrc){
eval(imgObj+'=new Image()');
eval(imgObj+'.src="'+imgSrc+'"');
}function ini(){
//preload('imageA','14.jpg');
//preload('imageB','15.jpg');
//preload('imageC','16.jpg');
// preload('imageD','15.jpg');
//preload('imageE','14.jpg');
//preload('imageF','15.jpg');
//preload('imageG','14.jpg');
//preload('imageH','15.jpg');
//preload('imageI','14.jpg');
//preload('imageJ','15.jpg');
//preload('imageK','14.jpg');
//preload('imageL','15.jpg');
var theimage=new Array("14.jpg","15.jpg","16.jpg","2.jpg","8.jpg","11.jpg","22.jpg","23.jpg","33.jpg","35.jpg");
var the_img=document.getElementById("img1");
// the_img.src=theimage[9];
var thenum=Math.random();
thenum=thenum*10;
thenum=Math.floor(thenum);
the_img.src=theimage[thenum];
//getimgid();
} function getimgid(){
var the_img=document.getElementById("img1");
the_img.src=(the_img.src==imageA.src)?imageB.src:imageA.src;
// the_img.style.filter=alpha(opacity=90);
window.setTimeout("getimgid()",3000);
// the_img.src=imageA.src;
} function img2small(){
}
</script>
</head>
<body onload='ini()'>
<img src='14.jpg' id='img1' width='200' style='filter:alpha(opacity=30)'/></body>
</html>
当onclick的时候, img1 的 src 立即就变为了 2.gif
getimgid() 等于无效而2.gif的加载在
eval(imgObj+'=new Image()');
eval(imgObj+'.src="'+imgSrc+'"');
等效于
imgpreload[i]=new Image()
imgpreload[i].src=imgname[i]这个脚本,应该是翻转脚本不知道你有没有试过,如果第一幅是10*10 jpg 图
第二幅是 640*480 GIF 30帧动画
你翻转后,取宽度高度试一试.我试过 complete 属性,可还是不行.
complete 在第一次装载完毕时,就被置为true了.?????? 用timer 等待没错,可是用什么判断第二幅图已经显示上了呢?tks继续求.....
------------------------
<script>
function getWidth(objName) {
if(eval(objName+".readyState") != 'complete'){
setTimeout("getWidth('"+objName+"')",10)
return
}
return eval(objName+".width")
}
</script>
<img name=img1 src=image/163.gif onclick="this.src='http://www.csdn.net/images/biaoshi.gif';alert(getWidth(this.name))">
问题解决了,呵呵,要稍为改动一下,感谢。感谢大家。<script>
function getWidth(objName) {
if(eval(objName+".readyState") != 'complete'){
setTimeout("getWidth('"+objName+"')",10)
}else {
alert( eval(objName+".width") );
}
}
</script>
<img name=img1 src=163.gif onclick="this.src='http://www.online99.net/pic_rf/rf_show3.JPG';getWidth(this.name)">