码迷,mamicode.com
首页 > 其他好文 > 详细

链式动画

时间:2019-11-29 15:37:51      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:mouseover   fun   round   func   技术   char   alt   pad   bsp   

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>链式动画</title>
    <style>
        body,div{
            margin: 0;
            padding:0;
        }
        #div1{
            width:200px;
            height:100px;
            background-color:yellow;
            border:4px solid #000;
            opacity:0.3;
        }
    </style>
    <script>
        window.onload=function(){
            var oDiv =document.getElementById(div1);
            oDiv.onmouseover =function(){
                startMove(oDiv,width,400,function(){
                    startMove(oDiv,height,300,function(){
                        startMove(oDiv,opacity,100);
                    });
                });
            };
            oDiv.onmouseout = function(){
                startMove(oDiv,opacity,30,function(){
                    startMove(oDiv,height,100,function(){
                        startMove(oDiv,width,200);
                    })
                })
            }
        }

        function startMove(obj,attr,iTarget,fn){
            clearInterval(obj.timer);
            obj.timer = setInterval(function(){

                var icur = null;                // 1.属性值 变量
                if(attr == opacity){
                    icur = Math.round(parseFloat(getStyle(obj,attr))*100);
                }else{
                    icur = parseInt(getStyle(obj,attr));
                }

                var speed = (iTarget - icur)/8;  // 2.速度 变速
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

                if(icur == iTarget){             // 3.判断是否暂停 变化过程
                    clearInterval(obj.timer);
                    if(fn){                      //链式
                        fn();
                    }
                }else{
                    if(attr == opacity){
                        obj.style.opacity = (icur +speed)/100;
                    }else{
                        obj.style[attr] = icur + speed + px;
                    }
                }
            },30)
        }

        function getStyle(obj,attr){             //获取任意属性
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }else{
                return getComputedStyle(obj,false)[attr];
            }
        }
    </script>
</head>
<body>
    <div id="div1"></div>
</body>
</html>

 

技术图片

 

链式动画

标签:mouseover   fun   round   func   技术   char   alt   pad   bsp   

原文地址:https://www.cnblogs.com/Fourteen-Y/p/11957896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!