记得喝水
2015-12-06 12:27
为什么设置了clearInterval()没有效果了。
<!DOCTYPE html>
<html lang="en/zh">
<head>
<meta charset="UTF-8">
<title>JS动画</title>
<style type="text/css">
*{
padding: 0px;
margin: 0px;
}
#share{
width: 200px;
height: 200px;
background: #2dcb7c;
position: relative;
left:-200px;
}
#share span{
width: 60px;
height: 50px;
line-height: 50px;
background: #272822;
position: absolute;/*发现了一个秘密,只要用了定位,就可以把inline元素转换层block元素*/
right: -60px;
top: 75px;
text-align: center;
}
</style>
</head>
<body>
<div id="share">
<span>分享</span>
</div>
<!-- javascript代码 -->
<script type="text/javascript">
window.onload =function(){
var odiv=document.getElementById("share");
odiv.onmouseover=function(){
gos();
}
}
/*动画函数*/
var timer=null;
function gos(){
clearInterval(timer);
var odiv=document.getElementById("share");
timer=setInterval(function(){
if(odiv.offsetLeft ==-10){
clearInterval(timer);
}
odiv.style.left=odiv.offsetLeft+10+"px";
},30)
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en/zh">
<head>
<meta charset="UTF-8">
<title>JS动画</title>
<style type="text/css">
*{
padding: 0px;
margin: 0px;
}
#share{
width: 200px;
height: 200px;
background: #2dcb7c;
position: relative;
left:-200px;
}
#share span{
width: 60px;
height: 50px;
line-height: 50px;
background: #272822;
position: absolute;/*发现了一个秘密,只要用了定位,就可以把inline元素转换层block元素*/
right: -60px;
top: 75px;
text-align: center;
}
</style>
</head>
<body>
<div id="share">
<span>分享</span>
</div>
<!-- javascript代码 -->
<script type="text/javascript">
window.onload =function(){
var odiv=document.getElementById("share");
odiv.onmouseover=function(){
gos();
}
}
/*动画函数*/
var timer=null;
function gos(){
clearInterval(timer);
var odiv=document.getElementById("share");
timer=setInterval(function(){
if(odiv.offsetLeft >=-10){
clearInterval(timer);
}else{
odiv.style.left=odiv.offsetLeft+10+"px";}
},30)
}
</script>
</body>
</html>
改了一下
JS动画效果
113910 学习 · 1502 问题
相似问题