欢迎移步我的博客阅读:《实用的 CSS — 贝塞尔曲线(cubic-bezier)》

前言

在了解 cubic-bezier 之前,你需要对 CSS3 中的动画效果有所认识,它是 animation-timing-functiontransition-timing-function 中一个重要的内容。

本体

简介

cubic-bezier 又称三次贝塞尔,主要是为 animation 生成速度曲线的函数,规定是 cubic-bezier(<x1>, <y1>, <x2>, <y2>)

我们可以从下图中简要理解一下 cubic-bezier

从上图我们需要知道的是 cubic-bezier 的取值范围:

  • P0:默认值 (0, 0)

  • P1:动态取值 (x1, y1)

  • P2:动态取值 (x2, y2)

  • P3:默认值 (1, 1)

我们需要关注的是 P1 和 P2 两点的取值,而其中 X 轴的取值范围是 01,当取值超出范围时 cubic-bezier 将失效;Y 轴的取值没有规定,当然也毋须过大。

最直接的理解是,将以一条直线放在范围只有 1 的坐标轴中,并从中间拿出两个点来拉扯(X 轴的取值区间是 [0, 1],Y 轴任意),最后形成的曲线就是动画的速度曲线

使用

在测试例子中:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>Document</title> <style>
.animation {
width: 50px;
height: 50px;
background-color: #ed3;
-webkit-transition: all 2s;
-o-transition: all 2s;
transition: all 2s;
}
.animation:hover {
-webkit-transform: translateX(100px);
-ms-transform: translateX(100px);
-o-transform: translateX(100px);
transform: translateX(100px);
}
</style>
</head>
<body>
<div class="animation"></div>
</body>
</html>

我们可以在浏览器中看到,当鼠标移到元素上时,元素开始向右移动,开始比较慢,之后则比较快,移开时按原曲线回到原点。

在例子中,当我们不为 transition 添加 cubic-bezier 或是其他 timing-function 时,默认的速度曲线是 ease,此时的速度曲线是:

那么让我们在代码中加入 cubic-bezier(.17, .86, .73, .14)

...
.animation {
...
-webkit-transition: all 2s cubic-bezier(.17, .86, .73, .14);
-o-transition: all 2s cubic-bezier(.17, .86, .73, .14);
transition: all 2s cubic-bezier(.17, .86, .73, .14);
}
...

再刷新页面观察效果,会看到动画在执行过程中有一段很缓慢的移动,前后的速度相似,此时的运动曲线是:

几个常用的固定值对应的 cubic-bezier 值以及速度曲线

  1. ease:cubic-bezier(.25, .1, .25, 1)

  2. liner:cubic-bezier(0, 0, 1, 1) / cubic-bezier(1, 1, 0, 0)

  3. ease-in:cubic-bezier(.42, 0, 1, 1)

  4. ease-out:cubic-bezier(0, 0, .58, 1)

  5. ease-in-out:cubic-bezier(.42, 0, .58, 1)

  6. In Out . Back(来回的缓冲效果):cubic-bezier(0.68, -0.55, 0.27, 1.55)

效果参考

文章所提到的动画效果可以在下面站点中看到,当然你也可以大胆尝试:

参考

MDN
W3School

本文转载于:猿2048实用的 CSS — 贝塞尔曲线(cubic-bezier)

最新文章

  1. nginx安装与配置
  2. PHP初步(中)
  3. libev学习(一)
  4. JQuery Easy Ui dataGrid 数据表格
  5. python之路一
  6. JProfiler使用入门(一)——准备工作
  7. EntityFramework 5.0 CodeFirst 教程04-查询,插入,更新,和删除数据
  8. LA 4119 (差分数列 多项式) Always an integer
  9. sqlserver 变量
  10. 别再说&ldquo;我已经努力了&rdquo;,你的&ldquo;努力&rdquo;一文不值!
  11. 转:Grunt:任务自动管理工具
  12. tomcat 控制台中文乱码问题
  13. mysql生成日期的辅助表
  14. CAFFE在win10+VS2017下的安装笔记
  15. Vue + Element UI 实现权限管理系统 前端篇(十二):用户管理模块
  16. 【windows核心编程】双机调试操作
  17. 【翻译自mos文章】在10g中,当发生ORA-00020时,sqlplus登陆会报“connected to an idle instance”
  18. HBase单机模式安装
  19. 史上最全Spring面试71题与答案
  20. 12、Redis的事务

热门文章

  1. PyTorch深度学习实践——反向传播
  2. 【高并发】两种异步模型与深度解析Future接口
  3. 如何使用在线工具手动验证JWT签名
  4. 在java的继承中你是否有这样的疑惑?
  5. Angular + Github action + Nginx 部署
  6. php 23种设计模型 - 模板方法模式
  7. 原生JS实现拼图游戏
  8. 详解Nacos 配置中心客户端配置缓存动态更新的源码实现
  9. (acwing蓝桥杯c++AB组)1.2 递推
  10. rodert教你学FFmpeg实战这一篇就够了