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.pswp.cn/news/393884.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393884.shtml
英文地址,請注明出處:http://en.pswp.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…