css弹性动画

通过css让生硬的动画变得更加自然

下面我们会通过javaScriptcss分别实现

效果图:

我们知道没一个物体运动都有一个运动轨迹,例如上面的小球是模拟的弹簧运动从而让动画更加自然,而上图的运动轨迹图

如下: (easeOutElastic)

我们就可以根据这个动画曲线得到他每个时间点的位置就形成了动画

常用的运动曲线

https://easings.net/ 好像是要安全上网才可以访问

我们随便点击一个(easeOutElastic)进去

注意:参数x表示运行的进度

我们会得到这样的一个公式,我们就可以通过这个公式加上scss完成一个动画

源码

ps:安装scss和mathsass(sass扩展函数这样就可以支持pow和sin这样类似的函数),vue-cli

<template>
  <div class="home">
    <pre>
      通过css的写法(测试是帧率为60fps)
    </pre>
    <div class="ball"></div>
  </div>
</template>

<style lang="scss" scoped>
@import "mathsass";
.ball {
  width: 50px;
  height: 50px;
  background-image: radial-gradient(circle, green, #abd500);
  border-radius: 50%;
  animation: 5s moveAni linear;
}

// easeOutElastic
@function elasticAniFn($t) {
  $c4: (2 * $PI) / 3;
  @if ($t == 0) {
    @return 0 * 400px;
  }
  @if ($t == 1) {
    @return 1 * 400px;
  }

  @return (pow(2, -10 * $t) * sin(($t * 10 - 0.75) * $c4) + 1) * 400px;
}

@keyframes moveAni {
  @for $i from 0 through 100 {
    #{$i * 1%} {
      transform: translate3d(elasticAniFn($i / 100), 0, 0);
    }
  }
}
</style>

解析:

在sass中

  • $代表变量,
  • #{}代表里面是表达式
  • @for $i from 0 through 100 循环
  • transform: translate3d(elasticAniFn($i / 100), 0, 0); 这个是移动的位置,使用translate3d:防止回流,重绘

JS的实现方法

https://github.com/zhangxinxu/Tween

 各大动画的js代码

  <script>
    /*
     * Tween.js
     * t: current time(当前时间);
     * b: beginning value(初始值);
     * c: change in value(变化量);
     * d: duration(持续时间)。
     * you can visit 'https://www.zhangxinxu.com/study/201612/how-to-use-tween-js.html' to get effect
    */
    var Tween = {
      Linear: function (t, b, c, d) {
        return c * t / d + b;
      },
      Quad: {
        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;
        },
        easeInOut: function (t, b, c, d) {
          if ((t /= d / 2) < 1) return c / 2 * t * t + b;
          return -c / 2 * ((--t) * (t - 2) - 1) + b;
        }
      },
      Cubic: {
        easeIn: function (t, b, c, d) {
          return c * (t /= d) * t * t + b;
        },
        easeOut: function (t, b, c, d) {
          return c * ((t = t / d - 1) * t * t + 1) + b;
        },
        easeInOut: function (t, b, c, d) {
          if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
          return c / 2 * ((t -= 2) * t * t + 2) + b;
        }
      },
      Quart: {
        easeIn: function (t, b, c, d) {
          return c * (t /= d) * t * t * t + b;
        },
        easeOut: function (t, b, c, d) {
          return -c * ((t = t / d - 1) * t * t * t - 1) + b;
        },
        easeInOut: function (t, b, c, d) {
          if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
          return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
        }
      },
      Quint: {
        easeIn: function (t, b, c, d) {
          return c * (t /= d) * t * t * t * t + b;
        },
        easeOut: function (t, b, c, d) {
          return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
        },
        easeInOut: function (t, b, c, d) {
          if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
          return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
        }
      },
      Sine: {
        easeIn: function (t, b, c, d) {
          return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
        },
        easeOut: function (t, b, c, d) {
          return c * Math.sin(t / d * (Math.PI / 2)) + b;
        },
        easeInOut: function (t, b, c, d) {
          return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
        }
      },
      Expo: {
        easeIn: function (t, b, c, d) {
          return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
        },
        easeOut: function (t, b, c, d) {
          return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
        },
        easeInOut: function (t, b, c, d) {
          if (t == 0) return b;
          if (t == d) return b + c;
          if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
          return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
        }
      },
      Circ: {
        easeIn: function (t, b, c, d) {
          return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
        },
        easeOut: function (t, b, c, d) {
          return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
        },
        easeInOut: function (t, b, c, d) {
          if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
          return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
        }
      },
      Elastic: {
        easeIn: function (t, b, c, d, a, p) {
          var s;
          if (t == 0) return b;
          if ((t /= d) == 1) return b + c;
          if (typeof p == "undefined") p = d * .3;
          if (!a || a < Math.abs(c)) {
            s = p / 4;
            a = c;
          } else {
            s = p / (2 * Math.PI) * Math.asin(c / a);
          }
          return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
        },
        easeOut: function (t, b, c, d, a, p) {
          var s;
          if (t == 0) return b;
          if ((t /= d) == 1) return b + c;
          if (typeof p == "undefined") p = d * .3;
          if (!a || a < Math.abs(c)) {
            a = c;
            s = p / 4;
          } else {
            s = p / (2 * Math.PI) * Math.asin(c / a);
          }
          return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b);
        },
        easeInOut: function (t, b, c, d, a, p) {
          var s;
          if (t == 0) return b;
          if ((t /= d / 2) == 2) return b + c;
          if (typeof p == "undefined") p = d * (.3 * 1.5);
          if (!a || a < Math.abs(c)) {
            a = c;
            s = p / 4;
          } else {
            s = p / (2 * Math.PI) * Math.asin(c / a);
          }
          if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
          return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
        }
      },
      Back: {
        easeIn: function (t, b, c, d, s) {
          if (typeof s == "undefined") s = 1.70158;
          return c * (t /= d) * t * ((s + 1) * t - s) + b;
        },
        easeOut: function (t, b, c, d, s) {
          if (typeof s == "undefined") s = 1.70158;
          return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
        },
        easeInOut: function (t, b, c, d, s) {
          if (typeof s == "undefined") s = 1.70158;
          if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
          return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
        }
      },
      Bounce: {
        easeIn: function (t, b, c, d) {
          return c - Tween.Bounce.easeOut(d - t, 0, c, d) + b;
        },
        easeOut: function (t, b, c, d) {
          if ((t /= d) < (1 / 2.75)) {
            return c * (7.5625 * t * t) + b;
          } else if (t < (2 / 2.75)) {
            return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
          } else if (t < (2.5 / 2.75)) {
            return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
          } else {
            return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
          }
        },
        easeInOut: function (t, b, c, d) {
          if (t < d / 2) {
            return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;
          } else {
            return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
          }
        }
      }
    }
    Math.tween = Tween;

实现代码

<template>
  <div class="about">
    <pre>通过js的写法</pre>
    <div class="ball" ref="bull"></div>
    <div @click="elasticFall">点击执行动画</div>
  </div>
</template>
<script>
export default {

  methods: {
    /*
     * Tween.js
     * t: current time(当前时间);
     * b: beginning value(初始值);
     * c: change in value(变化量);
     * d: duration(持续时间)。
    */
    easeOut (t, b, c, d) {
      if ((t /= d) < (1 / 2.75)) {
        return c * (7.5625 * t * t) + b;
      } else if (t < (2 / 2.75)) {
        return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
      } else if (t < (2.5 / 2.75)) {
        return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
      } else {
        return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
      }
    },
    /**
     * 动画执行函数
     */
    elasticFall () {
      console.log(this, '我执行了动画')
      let start = 0 // 当前时间
      let beginingValue = 0 // 初始值
      let changeValue = 400 // 变化量
      let during = 100; // 持续时间
      const _run = () => {
        start++
        var top = this.easeOut(start, beginingValue, changeValue, during);
        this.$refs.bull.style.webkitTransform = "translateY(" + top + "px)";
        if (start < during) {
          // 动画开始
          requestAnimationFrame(_run);
          // requestAnimationFrame的兼容处理
          // if (!window.requestAnimationFrame) {
          //   requestAnimationFrame = function (fn) {
          //     setTimeout(fn, 17);
          //   };
          // }
        } else {
          // 动画结束
          return
        }
      }
      _run();
    }
  }

}
</script>

<style lang="scss" scoped>
.ball {
  width: 50px;
  height: 50px;
  background-image: radial-gradient(circle, green, #abd500);
  border-radius: 50%;
}
</style>

解析

requestAnimationFrame:他会根据当前浏览器的最适合的帧率(时间点去完成动画),就是上一个动画完成下一个动画就会跟上。因为requestAnimationFrame和跟setTimeout非常类似,他们在js的任务队列的执行方式都是上一个队列执行完,才执行下一个,也就是上一个帧执行完才执行下一个,保证动画的连续性 具体区别:js的执行阶段

效果:

  • https://cubic-bezier.com/#.1,.68,.17,.85 网址关于贝塞尔曲线设置的网址,创建一些自定义的动画函数(cubic-bezier(n,n,n,n))
  •  http://jeremyckahn.github.io/stylie/ 网址可以直接导出代码
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值