一般项目里面需要用的类似gif动效的地方,我都是让ui直接切好图片给我的,要是他们知道可以用css实现会不会打死我(逃…
话不多说,先上个demo,这两个动画都是纯css实现的哦,甚至没有js的参与,是不是如丝滑般流畅…
下面我们来分析一下如何实现css帧动画
巧妇难为无米之炊,我们需要类似这张的素材,当然还是需要ui小姐姐帮忙了
让他动起来的原理,利用 keyframes,修改背景图的 background-position
以手机滑动的那个demo为栗子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <div class="animation2"></div>
.animation2 { width: 280px; height: 340px; background-image: url('./guide.png'); background-size: auto 340px; animation: swiper 1s steps(48, start) 0ms infinite normal backwards; margin-top: 42px; }
@keyframes swiper { 0% { background-position: 0 0; }
100% { background-position: -13440px 0; } }
|
分别说一下animation各属性的涵义
1 2 3 4 5 6 7 8 9
| animation: swiper 1s steps(48, start) 0ms infinite normal backwards;
animation-name: swiper; animation-duration: 1s; animation-timing-function: steps(48, start); animation-delay: 0ms; animation-iteration-count: infinite; animation-direction: normal; animation-fill-mode: backwards;
|