慕用9244143
2017-04-03 22:33
<!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=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.box{
width:100px;
height:100px;
background:#CF6;
border:2px solid;
position:relative;
left:-50px;
}
</style>
</head>
<body>
<div class="box">
</div>
<script type="text/javascript">
window.onload=function(){
var box=document.getElementsByClassName('box');
box.onmouseover=function(){
startMove();
}
}
function startMove(){
setInterval(function(){
var box=document.getElementsByClassName('box');
box.style.left=box.offsetLeft+10+'px';
},30)
}
</script>
</body>
</html>上面的代码没有效果,如果把box改成id的就可以了,这是为什么呢
你要是非要这么用的话,改写成var box=document.getElementsByClassName('box')[0];
不加[0]的话,box得到的是一个dom对象数组,之后需要for循环遍历,你再回头把js关于dom章节的知识点看看吧,基础知识不掌握就急着做效果是不行的
JS动画效果
113910 学习 · 1502 问题
相似问题