首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

为嘛小弟我的图片不移动。

2012-02-23 
为嘛我的图片不移动。。。。!DOCTYPEhtmlPUBLIC-//W3C//DTDXHTML1.0Transitional//EN http://www.w3.org/T

为嘛我的图片不移动。。。。
<!DOCTYPE   html   PUBLIC   "-//W3C//DTD   XHTML   1.0   Transitional//EN "   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html   xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 "   />
<title> 无标题文档 </title>
</head>

<body     onload= "initSoccerBall(); ">
<img   src= "20073315204866045.jpg "   width= "200 "   height= "150 "   id= "soccerBall "   />
</body>

<script   language= "javascript ">
function   initSoccerBall()
{
document.getElementById( "soccerBall ").animationTimer   =   setInterval( 'moveObject(document.getElementById( "soccerBall "),500,0,25) ',50);
}

function   moveObject(target,destinationLeft,destinationTop,maxSpeed)
{

var   currentLeft   =   parseInt(retrieveComputedStyle(target, "left "));
var   currentTop   =   parseInt(retrieveComputedStyle(target, "top "));
if(isNaN(currentLeft))
{
currentLeft   =   0;
}

if(isNaN(currentTop))
{
currentTop   =   0;
}

if(currentLeft   <   destinationLeft)
{
currentLeft   +=   maxSpeed;
if(currentLeft   >   destinationLeft)
{
currentLeft   =   destinationLeft;
}
}
else
{
currentLeft   -=   maxSpeed;
if   (currentLeft   <   destinationLeft)
{
currentLeft   =   destinationLeft;
}
}

if(currentTop   <   destinationTop)
{
currentTop   +=   maxSpeed;

if(currentTop   >   destinationTop)
{
currentTop   =   destinationTop;
}
}
else
{
currentTop   -=   maxSpeed
if(currentTop   <   destinationTop)
{
currentTop   =   destinationTop;
}
}

target.style.left   =   eval(currentLeft   +   "px ");
target.style.top   =   eval(currentTop   +   "px ");
if(currentLeft   ==   destinationLeft   &&   currentTop   ==   destinationTop)
{
clearInterval(target.animationTimer);
}
}

function   retrieveComputedStyle(target,position)
{
var   num   =   null;
if   (position   ==   "left ")
{
num   =   target.offsetLeft;
}
else
{
num   =   target.offsetTop;
}
return   num;
}


</script>
</html>



[解决办法]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 无标题文档 </title>
<script language= "javascript ">
function initSoccerBall()
{
document.getElementById( "soccerBall ").animationTimer = setInterval( 'doit() ',50);


}
function doit(){
moveObject(document.getElementById( "soccerBall "),500,0,25)
}
function moveObject(target,destinationLeft,destinationTop,maxSpeed)
{

var currentLeft = parseInt(retrieveComputedStyle(target, "left "));
var currentTop = parseInt(retrieveComputedStyle(target, "top "));
if(isNaN(currentLeft))
{
currentLeft = 0;
}

if(isNaN(currentTop))
{
currentTop = 0;
}

if(currentLeft < destinationLeft)
{
currentLeft += maxSpeed;
if(currentLeft > destinationLeft)
{
currentLeft = destinationLeft;
}
}
else
{
currentLeft -= maxSpeed;
if (currentLeft < destinationLeft)
{
currentLeft = destinationLeft;
}
}

if(currentTop < destinationTop)
{
currentTop += maxSpeed;

if(currentTop > destinationTop)
{
currentTop = destinationTop;
}
}
else
{
currentTop -= maxSpeed
if(currentTop < destinationTop)
{
currentTop = destinationTop;
}
}

target.style.left = parseInt(currentLeft);
target.style.top = parseInt(currentTop);
if(currentLeft == destinationLeft && currentTop == destinationTop)
{
clearInterval(target.animationTimer);
}
}

function retrieveComputedStyle(target,position)
{
var num = null;
if (position == "left ")
{
num = target.offsetLeft;
}
else
{
num = target.offsetTop;
}
return num;
}


</script>

</head>

<body onload= "initSoccerBall(); ">
<img src= "20073315204866045.jpg " width= "200 " height= "150 " id= "soccerBall " style= "position:absolute;z-index:2;left:15px;top:150px; "/>
</body>
</html>

热点排行