javascript 时间版运动框架案例

js部分


 var timer = null;
 var div = document.getElementsByTagName("div");
 function getStyle(obj, style) {
     if(window.getComputedStyle) {
         return window.getComputedStyle(obj,null)[style];
     }else {
         return obj.currentStyle[style];
     }
 }
 var targetObj = {
     width: 400,
     height: 400,
     opacity: 30,
     top: 400,
     left: 400
 }
 div[0].onmouseenter = function() {
     animate(this, targetObj, 'easeOut', 500, function() {
         animate(div[1], targetObj, 'easeOut', 500)
     });
 }
 function now() {
     return (new Date()).getTime();
 }
 // 运动对象、目标位置大小、运动函数、运动时间、回调
 function animate(obj, json, fx, times, callback) {
     var iCur = {};
     for(var attr in json) {
         if(attr == 'opacity') {
             iCur[attr] = Math.ceil(getStyle(obj,attr) * 100);
         }else {
             iCur[attr] = parseInt(getStyle(obj,attr));
         }
     }
     var startTime = now();
     clearInterval(obj.timer);
     obj.timer = setInterval(function(){
         var changeTime = now();
         var t = times - Math.max(0,startTime - changeTime + times);
         for(var attr in json) {
             var value = Tween[fx](t,iCur[attr],json[attr] - iCur[attr],times);                    
             if(attr == 'opacity') {
                 obj.style.opacity = value / 100;
             }else {
                 obj.style[attr] = value + 'px';
             }
         }
         if(t == times) {
             console.log(1)
             clearInterval(obj.timer);
             typeof callback == 'function' ? callback() : '';
         }
     },16)
 }

 // function startMove(obj, json, fx, times, callback) {
 //     clearInterval(obj.timer);
 //     var speed , cur;
 //     obj.timer = setInterval(function(){
 //         var stop = true;                
 //         for(var prop in json) {
 //             if(prop == "opacity") {
 //                 cur = parseFloat( getStyle(obj, prop) ) * 100;
 //             }else {
 //                 cur = parseInt( getStyle(obj, prop) );
 //             }
 //             speed = (json[prop] - cur) / 7;
 //             speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
 //             if(prop == "opacity") {
 //                 obj.style.opacity = (cur + speed) / 100;
 //             }else {
 //                 obj.style[prop] = (cur + speed) + "px";
 //             }
 //             if(cur != json[prop]) {
 //                 stop = false;
 //             }
 //         }
 //         if(stop) {
 //             clearInterval(obj.timer);
 //             typeof callback == "function" ? callback() : "";
 //         }
 //     },20)
 // }
 // div[0].onmouseenter = function() {
 //     startMove(this, targetObj, function() {
 //         startMove(div[1], targetObj)
 //     });
 // }

 var Tween = {
     linear : function(t,b,c,d) {   //匀速
         return c * t / d + b;
     },
     easeIn : function(t,b,c,d) {   //加速
         return c * (t /= d) * t + b;
     },
     easeOut : function(t,b,c,d) {  //减速
         return -c * (t /= d) * (t - 2) + b;
     },
     easeBoth : function(t,b,c,d) { //加速减速
         if((t /= d / 2) < 1) {
             return c / 2 * t * t + b;
         }
         return -c / 2 * ((--t) * (t - 2) - 1) + b;
     },
     easeInStrong : function(t,b,c,d) {  //加加速
         return c * (t /= d) * t * t * t + b;
     }
 }

html、css部分

<!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>
        * {
            margin: 0;
            padding: 0;
        }
        /* div {
            width: 100px;
            height: 100px;
            background-color: orange;
            /* position: absolute; */
            /* margin-bottom: 50px;
            border: 1px solid #000; */
            /* top: 0;
            left: 0; */
        /* } */ 
        .top {
            width: 100px;
            height: 100px;
            opacity: 1;
            background: orangered;
            position: absolute;
            top: 0;
            left: 0;
        }
        .bottom {
            position: absolute;
            width: 100px;
            height: 100px;
            background: orangered;
            opacity: 1;
            top: 300px;
            left: 0;
        }
    </style>
</head>
<body>
    <!-- <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div> -->
    <div class="top"></div>
    <div class="bottom"></div>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值