1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
< html  xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta  http-equiv = "Content-Type"  content = "text/html; charset=utf-8"  />
< title >无标题文档</ title >
< style >
#div1 {width:150px;height:200px;background:blue;left:-150px;top:100px;position:absolute;}
#div1 span{width:25px;height:50px;position:absolute;left:150px;top:75px;background:yellow;line-height:25x;}
</ style >
< script >
window. ()
{
var oDiv=document.getElementById('div1');
oDiv. ()
{
startMove(0);
};
oDiv. ()
{
startMove(-150);
};
};
 
var timer=null;
 
function startMove(n1)
{
var oDiv=document.getElementById('div1');
clearInterval(timer);
timer=setInterval(function ()
{
var speed=0;
//判断变框所处位置相对于目标位置,从而赋值速度大小
if(oDiv.offsetLeft>n1)
{
speed=-10;
}
else
{
speed=10;
}
if(oDiv.offsetLeft==n1)
{
clearInterval(timer);
}
else
{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},30);
};
 
</ script >
</ head >
 
< body >
< div  id = "div1" >
< span >分享</ span >
</ div >
</ body >
</ html >