javascript setTimeout()的问题...
想实现的效果为moveImg("img1",200,100,10);moveImg("img2",300,100,10);把2张图片移动到指定的left和top位置......
想实现的效果为
moveImg("img1",200,100,10);
moveImg("img2",300,100,10);
把2张图片移动到指定的left和top位置....
可是,各种问题.....
<script type="text/javascript">
window.onload=function()
{
move = window.setTimeout("positionImg()",1000);
}
function positionImg()
{
setPos("img1",10,10);
setPos("img2",20,20);
moveImg("img1",200,100,10);
moveImg("img2",300,100,10);
}
function moveImg(elementId,final_top,final_left,interval)
{
var imgs = document.getElementById(elementId);
imgs.style.position="absolute";
var xpos = parseInt(imgs.style.left);
var ypos = parseInt(imgs.style.top);
if(xpos==final_left&&ypos==final_top) return true;
if(xpos<final_left)
{
xpos+=2;
}
else
{
xpos-=2;
}
if(ypos<final_top)
{
ypos+=2;
}
else
{
ypos-=2;
}
imgs.style.left=xpos+"px";
imgs.style.left=ypos+"px";
alert(xpos+" "+ypos);
var repeat = "moveImg('"+elementId+"',"+final_top+","+final_left+","+interval+")";
window.setTimeout(repeat,interval);
}
function setPos(temp1,x,y)
{
var elem = document.getElementById(temp1);
elem.style.position="absolute";
elem.style.left="xpx";
elem.style.top="ypx";
}
</script>
</head>
<body style="background-color:#000">
<img id="img1" src="images/imagesshow/big2.gif" />
<img id="img2" src="images/imagesshow/big1.gif" />
</body> 展开
moveImg("img1",200,100,10);
moveImg("img2",300,100,10);
把2张图片移动到指定的left和top位置....
可是,各种问题.....
<script type="text/javascript">
window.onload=function()
{
move = window.setTimeout("positionImg()",1000);
}
function positionImg()
{
setPos("img1",10,10);
setPos("img2",20,20);
moveImg("img1",200,100,10);
moveImg("img2",300,100,10);
}
function moveImg(elementId,final_top,final_left,interval)
{
var imgs = document.getElementById(elementId);
imgs.style.position="absolute";
var xpos = parseInt(imgs.style.left);
var ypos = parseInt(imgs.style.top);
if(xpos==final_left&&ypos==final_top) return true;
if(xpos<final_left)
{
xpos+=2;
}
else
{
xpos-=2;
}
if(ypos<final_top)
{
ypos+=2;
}
else
{
ypos-=2;
}
imgs.style.left=xpos+"px";
imgs.style.left=ypos+"px";
alert(xpos+" "+ypos);
var repeat = "moveImg('"+elementId+"',"+final_top+","+final_left+","+interval+")";
window.setTimeout(repeat,interval);
}
function setPos(temp1,x,y)
{
var elem = document.getElementById(temp1);
elem.style.position="absolute";
elem.style.left="xpx";
elem.style.top="ypx";
}
</script>
</head>
<body style="background-color:#000">
<img id="img1" src="images/imagesshow/big2.gif" />
<img id="img2" src="images/imagesshow/big1.gif" />
</body> 展开
1个回答
2012-01-11
展开全部
不知道你要的是啥效果, 做个一个你看看符合要求不!!
<script type="text/javascript">
window.onload=function()
{
init();
move = window.setTimeout("positionImg()",1000);
}
var war = 2;
var har = 2;
function init() {
block = blockDiv.style;
block.xpos = parseInt(block.left);
block.ypos = parseInt(block.top);
}
function moveTo(obj,x,y) {
obj.xpos = x;
obj.left = obj.xpos;
obj.ypos = y;
obj.top = obj.ypos;
}
function positionImg()
{
moveImg(block,500,800,10);
}
function moveImg(block,final_top,final_left,interval)
{
var imgs = block
var xpos = parseInt(imgs.left);
var ypos = parseInt(imgs.top);
final_left = document.body.clientWidth-30;
final_top = document.body.clientHeight-30;
if(xpos==final_left&&ypos==final_top) return true;
if(xpos<=0)
{
war = 2;
}
if(xpos>=final_left){
war = -2;
}
if(ypos<=0)
{
har = 2;
}
if(ypos>=final_top){
har = -2;
}
xpos = xpos+war;
ypos = ypos+har;
imgs.left=xpos;
imgs.top=ypos;
var repeat = "moveImg(block,"+final_top+","+final_left+","+interval+")";
window.setTimeout(repeat,interval);
}
function setPos(temp1,x,y)
{
var elem = document.getElementById(temp1);
elem.style.position="absolute";
elem.style.left=x;
elem.style.top=y;
}
</script>
</head>
<body style="background-color:#000">
<IMG id='blockDiv' SRC="/images/logo.gif" BORDER=0 STYLE="position:absolute;left:1;top:2;width:30;">
</body>
<script type="text/javascript">
window.onload=function()
{
init();
move = window.setTimeout("positionImg()",1000);
}
var war = 2;
var har = 2;
function init() {
block = blockDiv.style;
block.xpos = parseInt(block.left);
block.ypos = parseInt(block.top);
}
function moveTo(obj,x,y) {
obj.xpos = x;
obj.left = obj.xpos;
obj.ypos = y;
obj.top = obj.ypos;
}
function positionImg()
{
moveImg(block,500,800,10);
}
function moveImg(block,final_top,final_left,interval)
{
var imgs = block
var xpos = parseInt(imgs.left);
var ypos = parseInt(imgs.top);
final_left = document.body.clientWidth-30;
final_top = document.body.clientHeight-30;
if(xpos==final_left&&ypos==final_top) return true;
if(xpos<=0)
{
war = 2;
}
if(xpos>=final_left){
war = -2;
}
if(ypos<=0)
{
har = 2;
}
if(ypos>=final_top){
har = -2;
}
xpos = xpos+war;
ypos = ypos+har;
imgs.left=xpos;
imgs.top=ypos;
var repeat = "moveImg(block,"+final_top+","+final_left+","+interval+")";
window.setTimeout(repeat,interval);
}
function setPos(temp1,x,y)
{
var elem = document.getElementById(temp1);
elem.style.position="absolute";
elem.style.left=x;
elem.style.top=y;
}
</script>
</head>
<body style="background-color:#000">
<IMG id='blockDiv' SRC="/images/logo.gif" BORDER=0 STYLE="position:absolute;left:1;top:2;width:30;">
</body>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询