CSS动画快速介绍

Interested in learning CSS? Get my CSS Handbook

有兴趣学习CSS吗? 获取我的CSS手册

介绍 (Introduction)

An animation is applied to an element using the animation property.

使用animation属性将动画应用于元素。

.container { animation: spin 10s linear infinite;}

spin is the name of the animation, which we need to define separately. We also tell CSS to make the animation last 10 seconds, perform it in a linear way (no acceleration or any difference in its speed), and to repeat it infinitely.

spin是动画的名称,我们需要单独定义它。 我们还告诉CSS使动画持续10秒,以线性方式执行动画(无加速度或速度差异),然后无限重复播放。

You must define how your animation works using keyframes. Here’s an example of an animation that rotates an item:

您必须使用关键帧 定义动画的工作方式 。 这是旋转项目的动画的示例:

@keyframes spin { 0% {  transform: rotateZ(0); } 100% {  transform: rotateZ(360deg); }}

Inside the @keyframes definition you can have as many intermediate waypoints as you want.

@keyframes定义中,您可以根据需要设置@keyframes多个中间航路点。

In this case, we instruct CSS to make the transform property to rotate the Z axis from 0 to 360 grades, completing the full loop.

在这种情况下,我们指示CSS使transform属性将Z轴从0旋转到360度,从而完成完整循环。

You can use any CSS transform here.

您可以在此处使用任何CSS转换。

Notice how this does not dictate anything about the temporal interval the animation should take. This is defined when you use it via animation.

请注意,这如何不决定动画应采用的时间间隔。 通过animation使用时定义。

CSS动画示例 (A CSS Animations Example)

I want to draw four circles, all with a starting point in common, all 90 degrees distant from each other.

我想绘制四个圆,它们的起点都相同,并且彼此相距90度。

<div class="container">  <div class="circle one"></div>  <div class="circle two"></div>  <div class="circle three"></div>  <div class="circle four"></div></div>
body {  display: grid;  place-items: center;  height: 100vh;}
.circle { border-radius: 50%; left: calc(50% - 6.25em); top: calc(50% - 12.5em); transform-origin: 50% 12.5em; width: 12.5em; height: 12.5em; position: absolute; box-shadow: 0 1em 2em rgba(0, 0, 0, .5);}
.one,.three { background: rgba(142, 92, 205, .75);}
.two,.four { background: rgba(236, 252, 100, .75);}
.one { transform: rotateZ(0);}
.two { transform: rotateZ(90deg);}
.three { transform: rotateZ(180deg);}
.four { transform: rotateZ(-90deg);}

You can see them in this Glitch:

您可以在此故障中看到它们:

Let’s make this structure (all the circles together) rotate. To do this, we apply an animation on the container, and we define that animation as a 360 degrees rotation:

让我们旋转该结构(所有圆一起)。 为此,我们在容器上应用动画,并将该动画定义为360度旋转:

@keyframes spin { 0% {  transform: rotateZ(0); } 100% {  transform: rotateZ(360deg); }}
.container { animation: spin 10s linear infinite;}

See it here:

在这里看到它:

You can add more keyframes to have funnier animations:

您可以添加更多关键帧来制作更有趣的动画:

@keyframes spin { 0% {  transform: rotateZ(0); } 25% {  transform: rotateZ(30deg); } 50% {  transform: rotateZ(270deg); } 75% {  transform: rotateZ(180deg); } 100% {  transform: rotateZ(360deg); }}

See the example:

参见示例:

CSS动画属性 (The CSS animation properties)

CSS animations offers a lot of different parameters you can tweak:

CSS动画提供了许多可以调整的不同参数:

  • animation-name — the name of the animation which references an animation created using keyframes

    animation- name-引用使用关键帧创建的动画的动画的名称

  • animation-duration — how long the animation should last, in seconds

    animation-duration —动画应持续多长时间,以秒为单位

  • animation-timing-function — the timing function used by the animation (common values: linear, ease). Default: ease

    animation-timing-function —动画使用的计时功能(常用值:线性,缓动)。 默认值:缓解

  • animation-delay — optional number of seconds to wait before starting the animation

    animation-delay —开始动画之前等待的可选秒数

  • animation-iteration-count — how many times the animation should be performed. Expects a number, or infinite. Default: 1

    animation-iteration-count-动画应执行多少次。 需要一个数字或无限。 默认值:1

  • animation-direction — the direction of the animation. Can be normal, reverse, alternate or alternate-reverse. In the last 2, it alternates going forward and then backwards

    animation-direction —动画的方向 。 可以是正向,反向,交替或反向交替。 在最后2个中,它交替向前和向后

  • animation-fill-mode — defines how to style the element when the animation ends, after it finishes its iteration count number. None or backwards go back to the first keyframe styles. Forwards and both use the style that’s set in the last keyframe

    animation-fill-mode —定义动画结束迭代计数后动画结束时如何设置元素的样式。 没有或向后返回第一个关键帧样式。 转发,并且都使用上一个关键帧中设置的样式

  • animation-play-state — if set to paused, it pauses the animation. Default is running.

    animation-play-state(动画播放状态)—如果设置为“暂停”,它将暂停动画。 默认正在运行。

The animation property is a shorthand for all these properties, in this order:

animation属性是所有这些属性的简写,顺序如下:

.container {  animation: name             duration             timing-function             delay             iteration-count             direction             fill-mode             play-state;}

This is the example we used above:

这是我们上面使用的示例:

.container { animation: spin 10s linear infinite;}

CSS动画JavaScript事件 (JavaScript events for CSS Animations)

Using JavaScript, you can listen for the following events:

使用JavaScript,您可以侦听以下事件:

  • animationstart

    animationstart

  • animationend

    animationend

  • animationiteration

    animationiteration

Be careful with animationstart, because if the animation starts on page load, your JavaScript code is always executed after the CSS has been processed. Then the animation will already be started and you cannot intercept the event.

请注意animationstart ,因为如果动画是在页面加载时开始的,则始终在处理完CSS之后执行JavaScript代码。 然后,动画将已经开始,并且您无法截获该事件。

const container = document.querySelector('.container')container.addEventListener('animationstart', (e) => { //do something}, false)container.addEventListener('animationend', (e) => { //do something}, false)container.addEventListener('animationiteration', (e) => { //do something}, false)

Interested in learning CSS? Get my CSS Handbook

有兴趣学习CSS吗? 获取我的CSS手册

翻译自: https://www.freecodecamp.org/news/a-quick-introduction-to-css-animations-a1655375ec90/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393884.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2_sat

要求字典序的情况的话&#xff0c;爆搜 不要求的话 1:建图&#xff0c;有向边A--->B的意义为选择A则必须选择B&#xff0c;一般一个点的两种取值情况会拆点。 2:缩点。 3:建反向图&#xff0c;跑拓扑排序&#xff08;有说不用建再跑&#xff0c;但我不懂为什么&#xff09;。…

[Spark][Python]Spark 访问 mysql , 生成 dataframe 的例子:

[Spark][Python]Spark 访问 mysql , 生成 dataframe 的例子&#xff1a; mydf001sqlContext.read.format("jdbc").option("url","jdbc:mysql://localhost/loudacre")\ .option("dbtable","accounts").option("user&quo…

ffmpeg mac 批量脚本_使用批处理脚本(BAT)调用FFMPEG批量编码视频

使用批处理脚本(BAT)编码视频非常方便&#xff0c;尤其当视频序列非常多的时候&#xff0c;更是省了不少简单重复性劳动。只要学会批处理里面几个基本的命令就行了&#xff0c;感觉和c/c差不多。set&#xff1a;设置变量(注意&#xff1a;变量一般情况下是字符串&#xff0c;而…

单实例oracle ha,Oracle单实例启动多个实例

Oracle单实例启动多个实例多实例运行&#xff0c;单个实例就是一个数据库&#xff01;一个数据库对应多个实例是RAC。Linux建立oracle的实例步骤&#xff1a;1、在linux服务器的图形界面下&#xff0c;打开一个终端&#xff0c;输入如下的命令&#xff1b; xhost ###远程调用…

leetcode357. 计算各个位数不同的数字个数(回溯)

给定一个非负整数 n&#xff0c;计算各位数字都不同的数字 x 的个数&#xff0c;其中 0 ≤ x < 10n 。示例:输入: 2 输出: 91 解释: 答案应为除去 11,22,33,44,55,66,77,88,99 外&#xff0c;在 [0,100) 区间内的所有数字。代码 class Solution {int numbers0;public int …

Shell编程 之 for 循环

1. 语法结构 2. 案例 2.1 批量解压缩 #!/bin/bashcd /root/test/ ls *.tar.gz > ls.log ls *.tgz >> ls.logfor i in $( cat ls.log )dotar -zxf $i &> /dev/nulldone rm -rf ls.log ~ …

react实战课程_在使用React一年后,我学到的最重要的课程

react实战课程by Tomas Eglinskas由Tomas Eglinskas 在使用React一年后&#xff0c;我学到的最重要的课程 (The most important lessons I’ve learned after a year of working with React) Starting out with a new technology can be quite troublesome. You usually find …

化工原理物性参数_化工原理知识点总结整理

1一、流体力学及其输送1.单元操作&#xff1a;物理化学变化的单个操作过程&#xff0c;如过滤、蒸馏、萃取。2.四个基本概念&#xff1a;物料衡算、能量衡算、平衡关系、过程速率。3.牛顿粘性定律&#xff1a;FτAμAdu/dy&#xff0c;(F&#xff1a;剪应力&#xff1b;A&#…

leetcode1415. 长度为 n 的开心字符串中字典序第 k 小的字符串(回溯)

一个 「开心字符串」定义为&#xff1a;仅包含小写字母 [a, b, c]. 对所有在 1 到 s.length - 1 之间的 i &#xff0c;满足 s[i] ! s[i 1] &#xff08;字符串的下标从 1 开始&#xff09;。 比方说&#xff0c;字符串 "abc"&#xff0c;"ac"&#xff0c…

8、linux上安装hbase

1.基本信息 版本1.2.4安装机器三台机器账号hadoop源路径/opt/software/hbase-1.2.4-bin.tar.gz目标路径/opt/hbase -> /opt/hbase-1.2.4依赖关系无2.安装过程 1).使用hadoop账号解压到/opt/hadoop目录下并设置软连接&#xff1a; [rootbgs-5p173-wangwenting opt]# su hadoo…

c oracle 记录,ORACLE 19c 操作相关记录

#数据源导出导入#导出exp oracle/oraclelocalhost:1521/orcl file/home/oracle/dmp/oracle20191120.dmp owneroracle log/home/oracle/dmp/log.log#导入imp oracletest/oracletestlocalhost:1521/orcl file/home/oracle/dmp/oracle20191120.dmp fully ignorey log/home/oracle…

TensorFlow.js快速入门

by Pau Pavn通过保罗帕文(PauPavn) TensorFlow.js快速入门 (A quick introduction to TensorFlow.js) TensorFlow has been around for a while now. Until last month, though, it was only available for Python and a few other programming languages, like C and Java. A…

Mountain Number FZU-2109数位dp

Mountain NumberFZU-2109 题目大意&#xff1a;一个大于0的数字x&#xff0c;分写成xa[0]a[1]a[2][3]..a[n]的形式&#xff0c;&#xff08;比如x1234,a[0]1,a[1]2,a[3]3,a[3]4&#xff09;,Mountain Number要满足对于a[2*i1]要大于等于a[2*i]和a[2*i2]&#xff0c;给定范围l,r…

[10.5模拟] dis

题意&#xff1a;给你一个主串&#xff0c;两个分串&#xff0c;要求两个分串的距离最大&#xff0c;两个分串的距离定义为第一个分串的最右边的字符和第二个分串的最左边的字符之间的字符数 题解&#xff1a; 直接kmp匹配两个分串即可 注&#xff1a;kmp匹配时&#xff0c;当分…

什么是非集计模型_集计与非集计模型的关系

集计与非集计模型的关系Wardrop第一.第二平衡原理集计模型在传统的交通规划或交通需求预测中&#xff0c;通常首先将对象地区或群体划分为若干个小区或群体等特定的集合体&#xff0c;然后以这些小区或群体为基本单位&#xff0c;展开问题的讨论。因此&#xff0c;在建立模型或…

微软dns能做cname吗_为什么域的根不能是CNAME以及有关DNS的其他花絮

微软dns能做cname吗This post will use the above question to explore DNS, dig, A records, CNAME records, and ALIAS/ANAME records from a beginner’s perspective. So let’s get started.这篇文章将使用上述问题从初学者的角度探讨DNS &#xff0c; dig &#xff0c; A…

Java Timestamp Memo

timestamp的构造函数&#xff0c;把微妙作为纳秒存储&#xff0c;所以 Java.util.date.comepareTo(Timestamp) 结果肯定是1另外&#xff0c;​Timestamp.equal(object) 如果参数不是Timestamp&#xff0c;肯定返回false。Timestamps nanos value is NOT the number of nanoseco…

oracle虚拟机字符集,更改虚拟机上的oracle字符集

修改oracle上边的字符集,需要用到DBA数据库管理员的权限,再修改字符集时要注意到修改后的字符集只能范围变大(例如:当前的字符集是GBK,那你修改后可以是UTF-8就是说后者只能比前者大,不能小.因为字符集都是向下兼容的)步骤:第一步:使用DBA身份登录先以绕过日志的方式登录在以然…

mybaits自连接查询

看不太懂&#xff0c;先记录再查&#xff0c;有没有大大解释下 resultmap里的collection设置select字段&#xff0c;看着像递归&#xff0c;没见过这种用法&#xff0c;#{pid}从何而来&#xff1f; 转载于:https://www.cnblogs.com/haon/p/10808739.html

token要加编码decode吗_彻底弄明白Base64 编码

Base64 encoding/decoding常见于各种authentication和防盗链的实现当中。彻底搞懂它绝对提升团队troubleshooting的底气。我们从纯手工方式编码解码开始&#xff0c;然后看看学到的技能怎么样应用在实际的troubleshooting 中。准备工作&#xff1a;我们应知道一个byte有8个bits…