10.图像对象

JavaScript

  • 图像对象:

网页中的图像均会被自动看作图像对象,并依顺序,分别表示为document.images[0],document.images[1]...

  • 建立图像对象:

格式:

图像对象名称=new Image([宽度],[高度])   //px

  • 图像对象的属性:

border complete height hspace lowsrc name src vspace width

  • 图像对象的事件:

onAbort onError onKeyDown onKeyPress onKeyUop onLoad

例1:(预载)
<Script>

img0 = new Image();
img0.src = "images/snow0.gif";

img1 = new Image();
img1.src = "images/snow1.gif";

document.write ("已经读取两个图文件,但此时不显示。");

</Script>

例2:
<Script>
function img-preload(idx){
    eval("img"+idx+" = new Image()");
    eval("img"+idx+".src = 'images/snow"+idx+".gif'");
}

img-preload(0);
img-preload(1);
document.write ("已经读取两个图文件,但此时不显示。");

</Script>

例3:
<Script>
function img-preload(imgname,idx){
    eval("img"+idx+" = new Image()");
    eval("img"+idx+".src = 'images/"+imgname+".gif'");
}

img-preload("snow0",0);
img-preload("snow1",1);
document.write ("已经读取两个图文件,但此时不显示。");

</Script>