js学习笔记 -运动框架(一)

来源:互联网 发布:网络机顶盒看直播软件 编辑:程序博客网 时间:2024/05/29 16:06
object.offsetLeft 当前对象所处位置
停止定时器只是说明下次就不执行了,但本次还会执行


运动框架:
1在开始运动时关闭定时器

2把运动和停止隔开


<!DOCTYPE html>
<html>
<head>
<style>
#content {background-color: #7f7f7f;width:200px;height: 300px;position:absolute;left:-200px;}
#shareBtn {background-color: yellow;width:30px;height: 90px;position:absolute;left:200px;top:85px;text-align:center;line-height:30px;}
</style>
<title>share</title>
</head>
<body>
<div id="content">
<span  id="shareBtn" >分享到</span>
</div>
<script type="text/javascript">
window.onload=function()
{
var content=document.getElementById('content');
content.onmouseover=function()
{
share();
}
content.onmouseout=function(){
shareleave();
}
}
var timer=null;
function share(){
var content=document.getElementById('content');
clearInterval(timer);
timer=setInterval(function(){
  if(content.offsetLeft==0)
    {
    clearInterval(timer);
    }   
    else
{
content.style.left=content.offsetLeft+10+'px';
}
},30)
}
function shareleave(){
var content=document.getElementById('content');
clearInterval(timer);
timer=setInterval(function(){
  if(content.offsetLeft==-200)
    {
    clearInterval(timer);
    }   
    else
{
content.style.left=content.offsetLeft-10+'px';
}
},30)



}
</script>
</body>
</html>

0 0
原创粉丝点击