transform: scaleY() translate()
实现盒子弹跳opacity
实现光幕和阴影transform: rotateZ()
盖子飞行,翻转top left transform: translate()
星星运动大概的思路都有了开始动手css
// 盒子居中布局
.stars{
opacity: 0;
}
.box{
top:50%;
left:50%;
position:absolute;
transform: translate(-50%,-50%);
}
// 基础 keyframes 函数
@mixin keyframes($animationName) {
@keyframes #{$animationName} {
@content;
}
}
// 因为是 translate 布局,这里改变定位使用 translate
@mixin animate-bounce($name:'bounce', $time: 1.5s, $animateFunc: linear) {
animation: $name $time $$animateFunc;
@include keyframes($name) {
0% {
transform: scaleY(1) translate(-50%, -50%);
}
8% {
transform: scaleY(0.98) translate(-50%, -48%);
}
50% {
transform: scaleY(1) translate(-50%, -70%);
}
100% {
transform: scaleY(1) translate(-50%, -50%);
}
}
}
}
@mixin animate-box-cover-fly($name:'box-cover-fly', $time: 1.5s, $animateFunc: linear){
animation: $name $time $$animateFunc;
@include keyframes($name) {
0% {
top: 23%;
left: 50%;
}
100% {
top: 5%;
left: 62%;
transform: translate(-20%, -50%) rotateZ(26deg);
}
}
}
}
// 盒子弹跳
.box-bounce{
@include animate-bounce('bounce');
}
.box-cover-fly{
@include animate-box-cover-fly('box-cover-fly');
}
复制代码
写到这里时候发现代码量会不少,即便用函数的形式,复写动画路径,具体实现中,发现效果并很差也不能彻底复刻设计给的动画。html
而后 google 了一下动画的实现方法,发现了帧动画,和雪碧图
。虽然这两个知识点早就据说过,可是使用的时候都是分开使用的。结合起来的时候也能作动画效果,说作就作,先写一个 democss3
htmlsegmentfault
<div className="step-box" />
复制代码
@mixin animate-spite-box($width: 134px) {
background: url('https://uploads.codesandbox.io/uploads/user/abc364e4-ea65-4398-a1dc-df2645675b64/U59a-lightbulb.png') 0 0 no-repeat;
background-size: cover;
animation: spite-box 1s steps(4) infinite;
@include keyframes(spite-box) {
from {
background-position: -8*$width;
}
to {
background-position: -12*$width;
}
}
}
$box-width: 134px
$box-height: 113px;
.step-box{
width: $box-width;
height: $box-height;
@include animate-spite-box($box-width);
}
复制代码
@mixin keyframes($animationName) {
@keyframes #{$animationName} {
@content;
}
}
@mixin animate-spite-box($width: 134px) {
background: url('https://uploads.codesandbox.io/uploads/user/abc364e4-ea65-4398-a1dc-df2645675b64/U59a-lightbulb.png') 0 0 no-repeat;
background-size: cover;
animation: spite-box 1s steps(4) infinite;
@include keyframes(spite-box) {
from {
background-position: -8*$width;
}
to {
background-position: -12*$width;
}
}
}
.step-box{
width: 134px;
height: 113px;
@include animate-spite-box(134px);
}
复制代码