首先要知道什麼是CSS3動畫?而後才能作出本身想要的動畫效果。下面會經過3個簡單的Loading動畫效果來對CSS3 animation動畫作一個簡單介紹,但願對你有用。css
動畫是使元素從一種樣式逐漸變化爲另外一種樣式的效果。 您能夠改變任意多的樣式任意多的次數。 使用百分比來規定變化發生的時間,或用關鍵詞 "from" 和 "to",等同於 0% 和 100%。 0% 是動畫的開始,100% 是動畫的完成。html
要建立CSS3動畫,那麼首先就要了解@keyframes規則。@keyframes規則是建立動畫。 @keyframes規則內指定一個CSS樣式和動畫將逐步從目前的樣式更改成新的樣式。 當在 @keyframes 建立動畫,把它綁定到一個選擇器,不然動畫不會有任何效果。 指定至少這兩個CSS3的動畫屬性綁定向一個選擇器:規定動畫的名稱;規定動畫的時長。css3
Loading動畫1:<div class="point-loading">
<span class="point"></span>
<span class="point"></span>
<span class="point"></span>
<span class="point"></span>
<span class="point"></span>
<span class="point"></span>
<span class="point"></span>
<span class="point"></span>
</div>
<!-- html代碼 總共8個點 -->
/*css樣式代碼*/
/*定義動畫*/
@keyframes pointLoading {
0% {
transform: scale(1);
opacity: 1;
}
100% {
transform: scale(.3);
opacity: 0.5;
}
}
/*定義樣式*/
.point-loading {
width: 100px;
height: 100px;
position: relative;
margin: 0 auto;
margin-top: 150px;
margin-bottom: 100px;
}
.point-loading .point {
width: 20px;
height: 20px;
border-radius: 50%;
background: lightgreen;
position: absolute;
animation-name:pointLoading; /*綁定動畫*/
animation-duration:1s; /*綁定動畫完成一個週期所用時間*/
animation-iteration-count:infinite; /*動畫播放次數 默認是1,infinite無限次播放*/
}
/*nth-child:選擇器匹配屬於其父元素的第 N 個子元素;animation-delay:動畫延遲播放*/
.point-loading .point:nth-child(1) {
left: 0;
top: 50%;
margin-top: -10px;
animation-delay: 0.13s;
}
.point-loading .point:nth-child(2) {
left: 14px;
top: 14px;
animation-delay: 0.26s;
}
.point-loading .point:nth-child(3) {
left: 50%;
top: 0;
margin-left: -10px;
animation-delay: 0.39s;
}
.point-loading .point:nth-child(4) {
top: 14px;
right: 14px;
animation-delay: 0.52s;
}
.point-loading .point:nth-child(5) {
right: 0;
top: 50%;
margin-top: -10px;
animation-delay: 0.65s;
}
.point-loading .point:nth-child(6) {
right: 14px;
bottom: 14px;
animation-delay: 0.78s;
}
.point-loading .point:nth-child(7) {
bottom: 0;
left: 50%;
margin-left: -10px;
animation-delay: 0.91s;
}
.point-loading .point:nth-child(8) {
bottom: 14px;
left: 14px;
animation-delay: 1.04s;
}
複製代碼
Loading動畫2:web
<!-- html代碼-->
<div class="loading"></div>
/*css代碼*/
/*首先是定義動畫效果*/
@keyframes rotateLoading {
from {
transform:rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
/*定義基本樣式,綁定動畫,定義動畫屬性*/
.loading {
margin: 50px auto;
width: 100px;
height: 100px;
border-radius: 50%;
border: 10px solid rgba(0, 0, 0, 0.2);
border-top-color: #000;
position: relative;
animation-name: rotateLoading;
animation-timing-function: linear;
animation-duration: 1.1s;
animation-iteration-count: infinite;
}
複製代碼
Loading動畫3:性能
<!--html代碼 共5個柱狀 -->
<div class="pillar-loading">
<span class="pillar"></span>
<span class="pillar"></span>
<span class="pillar"></span>
<span class="pillar"></span>
<span class="pillar"></span>
</div>
/*css代碼*/
@keyframes pillarLoading {
0%,
100% {
background: lightgreen;
}
50% {
transform: scaleY(1.75);
background: lightblue;
}
}
.pillar-loading {
margin: 150px auto;
width: 60px;
display: flex;
justify-content: space-between;
}
.pillar-loading .pillar {
width: 8px;
height: 40px;
border-radius: 4px;
background: lightgreen;
animation-name: pillarLoading;
animation-iteration-count: infinite;
animation-duration: 1s;
animation-timing-function: ease;
}
.pillar-loading .pillar:nth-child(2){
animation-delay: 0.2s;
}
.pillar-loading .pillar:nth-child(3){
animation-delay: 0.4s;
}
.pillar-loading .pillar:nth-child(4){
animation-delay: 0.6s;
}
.pillar-loading .pillar:nth-child(5){
animation-delay: 0.8s;
}
複製代碼
以上3個動畫是Animation動畫的簡單示例。flex
下面再說一個動畫必備屬性 transform。動畫
transform 本意是變形,變換之意,在 CSS 中使用該屬性可對元素進行移動(translate)、旋轉(rotate)、縮放(scale)、傾斜(skew)等效果。因其有着各類特效及優良的性能,因此成爲動畫的標配。url
** 轉換方法**spa
一個簡單的小球動畫,鼠標移到小球上或者空白框內,小球開始作動畫,鼠標移出,動畫中止。<!-- html代碼 -->
<div class="box">
<div class="circle"></div>
</div>
/*CSS代碼*/
.box {
width: 600px;
height: 200px;
border: 1px solid #ccc;
background: #fff;
margin: 50px,auto
}
.circle {
width: 50px;
height: 50px;
background: blue;
border-radius: 50%;
margin: 75px,0;
transition: all 2s /*2s完成*/
}
.box:hover .circle {
background: red;
transform: translate(550px,0) /*沿x軸偏移550px*/
}
複製代碼
再來一個稍微難一點的。code
<!-- html代碼 -->
<a href="https://y.qq.com/n/yqq/album/002JRl3m16wLPL.html" class="playlist-item">
<div class="item-bd">
<img class="item-img" src="https://user-gold-cdn.xitu.io/2018/8/31/1658e70c78f71b6c?w=300&h=300&f=jpeg&s=44005" alt="">
<i class="icon-play"></i>
</div>
<div class="item-ft">
<h3 class="item-tt">漂洋過海來看你 OST 原聲輯</h3>
<p class="item-author">嚴藝丹</p>
</div>
</a>
/*css樣式代碼*/
.playlist-item {
display: block;
margin-top: 100px;
width: 300px;
background: rgba(0, 0, 0, .6);
}
.playlist-item:hover {
background: #31c27c;
}
.playlist-item .item-bd {
overflow: hidden;
position: relative;
}
.playlist-item .item-img {
width: 100%;
transition:all 0.75s;
}
.playlist-item .icon-play {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(.7);
width: 70px;
height: 70px;
background: url(http://coding.imweb.io/img/p3/transition-cover_play.png) no-repeat;
opacity: 0;
}
.playlist-item .item-bd:hover .item-img {
transform:scale(1.1);
}
.playlist-item .item-bd:hover .icon-play {
opacity: 0.8;
transform: translate(-50%, -50%) scale(1);
}
.playlist-item .item-ft {
color: #fff;
padding: 15px 10px;
text-align: center;
}
.playlist-item .item-tt {
font-size: 16px;
position: relative;
display: inline-block;
vertical-align: middle;
}
.playlist-item .item-tt::after {
content: "...";
width: 18px;
height: 18px;
font-size: 12px;
color: #fff;
border-radius: 50%;
border: 2px solid #fff;
position: absolute;
right: -25px;
top: 50%;
transform: translate(0, -50%);
line-height: 0.6;
box-sizing: border-box;
opacity: 0;
transition:all 0.75s;
}
.playlist-item .item-author {
color: #999;
}
.playlist-item:hover .item-author {
color: #c1e9d5;
}
.playlist-item:hover .item-tt::after {
opacity:1;
}
複製代碼
本文轉載自:www.jianshu.com/p/6f53cc7d8…
參考資料:CSS3 transform www.w3cplus.com/content/css…