SVG 实现的类弹性小球

<script type="text/javascript">
      /**
       *计算小球弹起距离,nl = 0.8 *l;
       */
      function bounceDistance(isfall){
           var fall= document.getElementById("fall");
           var up= document.getElementById("up");
           var fallparam={from:parseInt(fall.getAttribute("from")),to:parseInt(fall.getAttribute("to"))};
           var upparam={from:parseInt(up.getAttribute("from")),to:parseInt(up.getAttribute("to"))};
           var time=parseFloat(fall.getAttribute("dur"));
          // console.log(fallparam);
           //console.log(upparam); 
          
           if(isfall){//设定弹起高度
               var max = fallparam.to - fallparam.from;
               var nh = max*0.8;
               up.setAttribute("to",upparam.from-nh);
               fall.setAttribute("from",upparam.from-nh);
              
           }else{
               // up.setAttribute("dur",time*0.99+"s");
               // fall.setAttribute("dur",time*0.99+"s");
               // console.log(parseFloat(fall.getAttribute("dur")));
           }
          
          
      }
 </script>
<svg width="500"  height="500">
 
  <circle cx="225" cy="30" r="25" style="stroke: none; fill: #0000ff;">
    <animate id="fall" attributeName="cy" attributeType="XML"
             from="30"  to="470 "
             begin="0s;up.end" dur="1s"
             fill="freeze"
             repeatCount="" οnend="bounceDistance(true);"/>
    <animate id="up" attributeName="cy" attributeType="XML"
             from="470"  to="30"
             begin="fall.end" dur="1s"
             fill="freeze" 
             repeatCount="" οnend="bounceDistance(false);"/>
    </circle>
</svg>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值