-1
<HTML><head>
<Script language="JavaScript">
<!--
function changePos() 
{
 width = document.body.clientWidth;   //the width of the current document
 height = document.body.clientHeight; //the height of the current document
 Hoffset = img.offsetHeight;
 Woffset = img.offsetWidth;
 img.style.left = xPos;  
 img.style.top = yPos;  

 if (yon) 
 {
  yPos = yPos + 5;
 } 
 else 
 { 
  yPos = yPos - 5;
 }
if(yPos < -Hoffset) 
 {
  yon = 1; yPos = -Hoffset;
 }
 if(yPos >= height) 
 {
  yon = 0; yPos = height;
 }
}
//-->
</Script>
</head>
<body>
<IMG id="img" style="POSITION: absolute" src="Images\Colored.jpg">
<SCRIPT language=JavaScript>
<!-- 
 var height = 0;
 var Hoffset = 0;
 var Woffset = 0;
 var yon = 0;  //false
 var xPos = 20;
 var yPos = document.body.clientHeight;
 var Timer1 = setInterval('changePos()',30);
//-->
</Script>
</BODY>
</HTML>

所以我的问题是,我如何在这个动画中添加一个新图像,并以一种与旧图像交替的方式制作它(例如:我拥有的图像是彩色的,我想给它添加一个黑白图像,所以他们交替在一起!

4

2 回答 2

0

开始非常简单,您可以简单地创建一个图像数组:

var images = new Array( "image1.jpg",
        "image2.jpg",
        "image3.jpg");

然后在 html 中声明一个 img 元素,例如:

<img id="img1" src="image3.jpg" />

然后只需将其添加到您的函数中:

document.getElementById("img1").src = images[0] // or whatever image number you want.

看看这里:

http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/131227/sharing-images-in-html

于 2012-10-04T09:09:19.987 回答
0

也许是这样:

<html><head>
<script language="JavaScript"><!--
 var timer1,Hoffset = 0,Woffset = 0,
     xPos = 20,ypos,
     width = document.body.clientWidth,
     height=yPos=document.body.clientHeight,yon = 0;  //false

 window.onload=function() {
   Timer1 = setInterval(changePos,30);
 }

function changePos() {
  var img = document.getElementById('img');
  Hoffset = img.offsetHeight;
  Woffset = img.offsetWidth;

  yPos += (yon)?5:-5;
  if(yPos < -Hoffset)  {
   yon = 1; yPos = -Hoffset;
  }
  if(yPos >= height) {
   yon = 0; yPos = height;
  }
  img.src = img.src.indexOf("Colored.jpg"!=-1)?"Images\BW.jpg":"Images\Colored.jpg";
  img.style.left = xPos + "px";  
  img.style.top = yPos + "px";  
}
//-->
</script>
</head>
<body>
<img id="img" style="position: absolute" src="Images\Colored.jpg">
</body>
</html>
于 2012-10-04T09:14:10.157 回答