前言
2017年前端火了,微信小程序、weex、reactnative,就連支付寶也搞起了小程序,總感覺這是原生要?dú)绲墓?jié)奏啊,我也乘熱上車萬(wàn)一波。
上效果圖(GI動(dòng)態(tài)圖)

當(dāng)我看到這張背景圖的時(shí)候,強(qiáng)迫癥立馬來(lái)了,這云朵為什么不動(dòng),于是開始了一波折騰。
知識(shí)點(diǎn)
認(rèn)識(shí)animation
animation 屬性是一個(gè)簡(jiǎn)寫屬性,用于設(shè)置六個(gè)動(dòng)畫屬性:
值 描述
animation-name 規(guī)定需要綁定到選擇器的 keyframe 名稱。。
animation-duration 規(guī)定完成動(dòng)畫所花費(fèi)的時(shí)間,以秒或毫秒計(jì)。
animation-timing-function 規(guī)定動(dòng)畫的速度曲線。
animation-delay 規(guī)定在動(dòng)畫開始之前的延遲。
animation-iteration-count 規(guī)定動(dòng)畫應(yīng)該播放的次數(shù)。
animation-direction 規(guī)定是否應(yīng)該輪流反向播放動(dòng)畫。
認(rèn)識(shí)translate
方法特別多,本文主要用2個(gè)。
- translate3d(x,y,z)定義 3D 縮放轉(zhuǎn)換。
- rotate3d(x,y,z,angle) 定義 3D 旋轉(zhuǎn)。
translate3d(1,1,0)
你可以理解為(左右,上下,大小)變化。
rotate3d(1,1,0,45deg)

實(shí)現(xiàn)
1.兩朵云除了大小和初始位置不通,其他都相同。
.cloud {
position: absolute;
z-index: 3;
width:99px;height:64px; top: 0;
right: 0;
bottom: 0;
animation: cloud 5s linear infinite;
}
@keyframes cloud {
from {
transform: translate3d(-125rpx, 0, 0);
}
to {
transform: translate3d(180rpx, 0, 0);
}
}
其中rpx是微信特有的屬性,不受屏幕大小的影響,類似于安卓里的dp單位。keyframes是勻速移動(dòng),從css里可以看到只改變了左右方向。
2.頭像本來(lái)想加個(gè)吊籃,像蕩秋千一樣的蕩漾,但是沒有成功,只是隨便搞了個(gè)飄來(lái)飄去的動(dòng)畫。

代碼如下
@keyframes pic {
0% {
transform: translate3d(0, 20rpx, 0) rotate(-15deg);
}
15% {
transform: translate3d(0, 0rpx, 0) rotate(25deg);
}
36% {
transform: translate3d(0, -20rpx, 0) rotate(-20deg);
}
50% {
transform: translate3d(0, -10rpx, 0) rotate(15deg);
}
68% {
transform: translate3d(0, 10rpx, 0) rotate(-25deg);
}
85% {
transform: translate3d(0, 15rpx, 0) rotate(15deg);
}
100% {
transform: translate3d(0, 20rpx, 0) rotate(-15deg);
}
}
沒想到keyframes不僅有支持from to還支持百分比,不錯(cuò)。這里,只要控制好層級(jí)關(guān)系、動(dòng)畫時(shí)長(zhǎng)、透明度即可實(shí)現(xiàn)云層漂浮。
總結(jié)
不得不說(shuō)css還是有很多動(dòng)畫的,也有很多特效,微信小程序里加一點(diǎn)動(dòng)畫,能使頁(yè)面稍微美觀點(diǎn)。當(dāng)然,復(fù)雜點(diǎn)的動(dòng)畫,只能有機(jī)會(huì)再更新。
csdn博客:
http://blog.csdn.net/qq273681448/
簡(jiǎn)書:
http://www.lxweimin.com/users/ef6207f116a2/timeline
掘金:
https://juejin.im/user/57cd55218ac247006459c40c
Github:
https://github.com/qq273681448