分享到 代码

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <style>
 5 #div1 {width:100px; height:200px; background:#CCC; position:absolute; left:-100px;}
 6 #div1 span {width:20px; height:60px; line-height:20px; text-align:center; left:100px; top:70px; background:yellow; position:absolute;}
 7 
 8 </style>
 9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
10 <title>无标题文档</title>
11 <script type="text/javascript">
12 window.οnlοad=function ()
13 {
14     var oDiv=document.getElementById('div1');
15     
16     oDiv.οnmοuseοver=function ()
17     {    
18         //函数调用
19         startMove(0);
20     }
21     
22     oDiv.οnmοuseοut=function ()
23     {    
24         //函数调用
25         startMove(-100);
26     }
27 }
28 var timer=null;
29 
30 function startMove(iTarget)
31 {
32     var oDiv=document.getElementById('div1');
33     
34     clearInterval(timer);
35     timer=setInterval(function (){
36         //缓冲运动的实现
37         var iSpeed=(iTarget-oDiv.offsetLeft)/8;
38         
39         iSpeed = iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
40         
41         if(oDiv.offsetLeft==iTarget)
42         {
43             clearInterval(timer);
44         }
45         else
46         {
47             oDiv.style.left=oDiv.offsetLeft+iSpeed+'px';
48         }
49     }, 30);
50 }
51 </script>
52 </head>
53 
54 <body>
55 <div id="div1">
56     <span>分享到</span>
57 </div>
58 </body>
59 </html>

注:在发现有大量函数代码重复的时候,可以考虑使用参数,节省代码的空间。同时注意,在写css样式的时候对于运动来说一定要使用定位。

   缓冲运动中一定要注意函数的取整。

转载于:https://www.cnblogs.com/xs-yqz/p/4486806.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值