任意值的运动框架

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div{
            width:200px;
            height:200px;
            border:2px solid red;
            margin:10px;
            background-color: green;
            opacity:0.3;
            font-size: 14px;
            filter:alpha(opacity:30);     
        }
    </style>
</head>
<body>
   <div> 变宽</div>
   <div>变高</div>
   <div>fhhfshdjsd</div>
   <script>
     window.onload=function(){
         var div=document.getElementsByTagName('div');
            div[0].onmouseover=function(){
            //   startMove(this,'opacity',100);
             startMove(this,'width',500);
             //同一个函数调用两次 前一个会被覆盖

           }      
           div[1].onmouseover=function(){
             startMove(this,'height',900);
            //  console.log(this.style.height);
           } 
           div[2].onmouseover=function(){
               startMove(this,'fontSize',20);
           }
        

     }
     function getStyle(obj,name){//获取样式
         if(obj.currentStyle){
             return obj.currentStyle[name];
         }else{
             return getComputedStyle(obj,null)[name];
         }
     }
    //  var timer=null;
     function startMove(obj,name,target){
         clearInterval(obj.timer);
         obj.timer=setInterval(function(){
             var curr=0;//存贮获取样式的值
             if(name=='opacity'){ 
                 curr=Math.round(parseFloat(getStyle(obj,name))*100);
                 //Math.round解决小数问题
             }else{
                 curr=parseInt(getStyle(obj,name));
             }
            var speed=(target-curr)/6;//目标地址和当前地址不能写反
            speed=speed>0?Math.ceil(speed):Math.floor(speed);
             if(curr==target){
                 clearInterval(obj.timer);
             }else{
                 if(name=='opacity'){
                     curr=curr+speed;
                     obj.style.filter='alpha(opacity:+'+curr+')';
                     obj.style.opacity=curr/100;
                    //  console.log(curr);
                 }else{
                 obj.style[name]=curr+speed+'px';
                 }
             }
         },300);





     }

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值