一段图片js代码,如何不用到<body onload="mv()">这语句
<scripttype="text/javascript">varn=0;varx=2;functionmv(){varimgBox=document.getElemen...
<script type="text/javascript">
var n=0;var x = 2;
function mv(){var imgBox = document.getElementById('imgBox');
imgBox.style.left = n+"px";
n=n-x;
if(n<= -430){x=-x;}else if(n>=0){x=-x;}
setTimeout(mv,20);}
</script>
<body onload="mv()">
<div id="imgB">
<table id="imgBox" cellspacing="0" cellpadding="0" border="0">
<td><img src="image/1.jpg" width="215px" height="250px" /></td>
<td><img src="image/2.jpg" width="215px" height="250px" /></td>
<td><img src="image/3.jpg" width="215px" height="250px" /></td>
</table>
</div>
</body> 展开
var n=0;var x = 2;
function mv(){var imgBox = document.getElementById('imgBox');
imgBox.style.left = n+"px";
n=n-x;
if(n<= -430){x=-x;}else if(n>=0){x=-x;}
setTimeout(mv,20);}
</script>
<body onload="mv()">
<div id="imgB">
<table id="imgBox" cellspacing="0" cellpadding="0" border="0">
<td><img src="image/1.jpg" width="215px" height="250px" /></td>
<td><img src="image/2.jpg" width="215px" height="250px" /></td>
<td><img src="image/3.jpg" width="215px" height="250px" /></td>
</table>
</div>
</body> 展开
1个回答
展开全部
<script type="text/javascript">
var n=0;var x = 2;
function mv(){
var imgBox = document.getElementById('imgBox');
imgBox.style.left = n+"px";
n=n-x;
if(n<= -430){x=-x;}else if(n>=0){x=-x;}
setTimeout(mv,20);
}
//调用函数即不需要再body区域加onLoad事件
mv();
</script>
追问
运行了图片不会动
追答
将整段JS放到文档末尾曲,可能文档还没加载完成,imgBox不能正确获取。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询