过渡(transition)
过渡指定一个属性发生变化时的一个切换方式
过渡可以有一些非常好的体验,提升用户体验
属性
transition-property
指定执行过渡的属性transition-duration
过渡时间tansition-delay
过渡效果的延迟(等待一段时间在进行过渡)transition-timing-function
过渡的时序函数
-linear
匀速运动
-ease
默认值,慢速开始,先加速后减速
-ease-in
加速运动
-ease-in-out
先加速后减速
-cubic-bezier()
自己指定时序函数https://cubic-bezier.com
-steps()
分步执行过渡效果,第二个值有可选值(end
在时间结束时执行过渡 ,start
时间开始时执行过渡)
transition
同时设置过渡相关的所有属性
如果要写延迟,则两个时间中,第一个是持续时间,第二个是延迟时间
示例
/* transition: margin-left 2s 1s; */
transition-property: margin-left;
transition-duration: 2s;
transition-delay: 1s;
transition-timing-function: linear;
**过渡效果示例
**
steps()
分步执行过渡效果
transition-timing-function: steps(2);
transition-timing-function
指定时序
transition-timing-function: cubic-bezier(.17, 1.79, .68, -0.69);
动画
动画用于实现一些动态效果,于过渡的区别在于:
- 过渡需要在某个属性发生变化时才会触发
- 动画可以自动触发动态效果
设置动画效果,必须要设置一个关键帧,关键帧设置动画执行的每一个步骤
form 是开始, to是动画结束,百分比是指动画执行的进程(如执行到整个动画45%的地方就怎么怎么样)
//test 是关键帧的名称
@keyframes test {
//from 是动画的开始
from {
margin-left: 0;
}
//百分比指向动画执行过程中某个部分
45%{
margin-left: 50px;
}
//to是动画的结尾
to {
margin-left: 900px;
}
}
属性
animation-name
指定动画的关键帧名称
animation-duration
:指定动画效果的持续时间
animation-delay
:动画效果的延迟,等待一段时间后在执行动画
animation-timing-function
:动画的时序函数(跟过渡一样)
animation-iteration-count
动画执行的次数
infinite
无限执行
animation-direction
指定动画运行的方向
normal
从from向to运行,每次都是这样,默认值reverse
从to向from运行,每次都是这样alternate
从from向to运行,重复执行动画时反向执行alternate-reverse
从to向from运行,重复执行动画时反向执行
animation-play-state
设置动画的执行状态
running
动画执行,默认值paused
动画暂停
animation-fill-mode
动画的填充模式
none
动画执行完毕,元素回到原来位置,默认值forwards
动画执行完毕,元素会停止在动画结束的位置backwards
动画延时等待时,元素就会处于开始位置both
结合了forwards和backwards
示例
/* animation-name: test;
animation-duration: 2s;
animation-delay: 2s;
animation-timing-function: linear;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-fill-mode: both; */
animation: test 2s 2s linear infinite alternate both;
实战例子
1. 米兔
.box {
height: 271px;
width: 132px;
background-image: url("/assets/米兔/bigtap-mitu-queue-big.png");
margin: 100px auto;
transition: background-position 1s steps(4);
}
.box:hover {
background-position: -528px 0;
}
2.奔跑的少年
.box {
height: 256px;
width: calc(1536px/6);
background-image: url("/assets/奔跑的少年/bg2.png");
margin: 100px auto;
animation: run 1s steps(6) infinite;
}
/* 关键帧 */
@keyframes run {
from {
background-position: 0 0;
}
to {
background-position: -1536px 0;
}
}
3.弹力球
width: 100%;
height: 700px;
border-bottom: 10px solid #000;
/* 外边距重叠,开启BFC */
overflow: hidden;
}
.ball {
width: 100px;
height: 100px;
border-radius: 50%;
background-color: gray;
animation: bounce 6s ease-in;
}
@keyframes bounce {
from {
margin-top: 0;
}
5%,
15%,
25%,
35%,
45%,
55%,
65%,
75%,
85%,
95%,
98%,
to {
margin-top: 600px;
animation-timing-function: ease-out;
}
10%,
20%,
30%,
40%,
50%,
60%,
70%,
80%,
90% {
animation-timing-function: ease-in;
}
10% {
margin-top: 60px;
}
20% {
margin-top: 120px;
}
30% {
margin-top: 180px;
}
40% {
margin-top: 240px;
}
50% {
margin-top: 300px;
}
60% {
margin-top: 360px;
}
70% {
margin-top: 420px;
}
80% {
margin-top: 480px;
}
90% {
margin-top: 540px;
}
96% {
margin-top: 580px;
}
99% {
margin-top: 590px;
}
}
4. 酷炫球
div {
float: left;
width: 100px;
height: 100px;
border-radius: 50%;
animation: bounce .5s infinite ease-in alternate;
}
.ball1 {
background-color: red;
animation-delay: .1s;
}
.ball2 {
background-color: yellow;
animation-delay: .2s;
}
.ball3 {
background-color: green;
animation-delay: .3s;
}
.ball4 {
background-color: blue;
animation-delay: .4s;
}
.ball5 {
background-color: pink;
animation-delay: .5s;
}
.ball6 {
background-color: orange;
animation-delay: .6s;
}
.ball7 {
background-color: fuchsia;
animation-delay: .7s;
}
.ball8 {
background-color: gray;
animation-delay: .8s;
}
.ball9 {
background-color: darkcyan;
animation-delay: .9s;
}
.ball10 {
background-color: indigo;
animation-delay: 1s;
}
.ball11 {
background-color: black;
animation-delay: 1.1s;
}
.ball12 {
background-color: darkcyan;
animation-delay: 1.2s;
}
.ball13 {
background-color: darkkhaki;
animation-delay: 1.3s;
}
.ball14 {
background-color: brown;
animation-delay: 1.4s;
}
.ball15 {
background-color: mediumpurple;
animation-delay: 1.5s;
}
@keyframes bounce {
from {
margin-top: 0;
}
to {
margin-top: 500px;
}
}