//定时器
//刚进浏览器获取进行加载
window.onload=function(){
var oDiv=document.getElementById("abc");
//鼠标移入
oDiv.onmouseover=function(){
startMove(0);
}
oDiv.onmouseout=function(){
startMove(-200);
}
}
//定义startMove()函数
var timer=null;
function startMove(iTarget){
//刚进来把所有得定时器清除掉
clearInterval();
var oDiv=document.getElementById("abc");
//定时器
timer=setInterval(function(){
var speed=0;
if(oDiv.offsetLeft>iTarget){
speed=-10;
}
else{
speed=10;
}
if(oDiv.offsetLeft==iTarget){
clearInterval(timer);
}
else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30)
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>js动画</title>
<script type="text/javascript" src="js/js.js" ></script>
<style>
body,div,span{
margin: 0;
padding: 0;
}
#abc{
width: 200px;
height: 200px;
background: red;
position: relative;
left: -200px;
top: 0;
}
#abc span{
width: 20px;
height: 50px;
background: blue;
position: absolute;
left: 200px;
top: 75px;
color: #fff;
}
</style>
</head>
<body>
<div id="abc">
<span id="share">分享</span>
</div>
</body>
</html>
慕数据5775487
相关分类