1.关键帧动画
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
div {
width : 200px;
height : 200px;
background : red;
animation : kerwinrun2 5s linear infinite;
}
@keyframes kerwinrun1 {
from {
width : 200px;
height : 200px;
background : red;
}
to {
width : 400px;
height : 600px;
background : green;
}
}
@keyframes kerwinrun2 {
0% {
width : 200px;
height : 200px;
background : red;
}
25% {
width : 200px;
height : 400px;
background : yellow;
}
50% {
width : 400px;
height : 400px;
background : blue;
}
75% {
width : 400px;
height : 500px;
background : gray;
}
100% {
width : 400px;
height : 600px;
background : green;
}
}
</ style>
</ head>
< body>
< div> </ div>
</ body>
2.animation(单一属性)
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
div {
width : 200px;
height : 200px;
background : red;
animation-name : kerwinrun1;
animation-duration : 2s;
animation-timing-function : linear;
animation-iteration-count : infinite;
animation-play-state : running;
animation-direction : alternate-reverse;
}
div:hover {
animation-play-state : paused;
}
@keyframes kerwinrun1 {
from {
width : 200px;
height : 200px;
background : red;
}
to {
width : 400px;
height : 600px;
background : green;
}
}
@keyframes kerwinrun2 {
0% {
width : 200px;
height : 200px;
background : red;
}
25% {
width : 200px;
height : 400px;
background : yellow;
}
50% {
width : 400px;
height : 400px;
background : blue;
}
75% {
width : 400px;
height : 500px;
background : gray;
}
100% {
width : 400px;
height : 600px;
background : green;
}
}
</ style>
</ head>
< body>
< div> </ div>
</ body>
其他属性
抽屉动画–animation-fill-mode(最后一帧的定格)
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
* {
margin : 0;
padding : 0;
}
html,body {
height : 100%;
}
div {
height : 100%;
width : 200px;
background : red;
position : fixed;
left : 0px;
top : 0px;
transform : translateX ( -100%) ;
animation : run 1s linear forwards reverse;
}
@keyframes run {
from {
transform : translateX ( -100%) ;
}
to {
transform : translateX ( 0) ;
}
}
</ style>
</ head>
< body>
< div> </ div>
</ body>
steps(直接进行关键帧跳跃),linear,ease-in,去除传统补间
steps(1,start)等价于step-start steps(1,end)等价于step-end
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
div {
width : 200px;
height : 200px;
background : red;
animation : run 5s step-end;
}
@keyframes run {
0% {
background : red;
}
50% {
background : green;
}
100% {
background : yellow;
}
}
</ style>
</ head>
< body>
< div> </ div>
</ body>
3.轮播图案例
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
* {
margin : 0;
padding : 0;
}
.swiper-container {
width : 640px;
height : 300px;
margin : 0 auto;
overflow : hidden;
}
.swiper-container img {
width : 640px;
height : 300px;
}
.swiper-slide {
float : left;
}
.swiper-wrapper {
width : 9999px;
animation : swiperrun 10s linear infinite;
}
.swiper-wrapper:hover {
animation-play-state : paused;
}
@keyframes swiperrun {
0% {
transform : translateX ( 0) ;
}
5% {
transform : translateX ( -640px) ;
}
25% {
transform : translateX ( -640px) ;
}
30% {
transform : translateX ( -1280px) ;
}
50% {
transform : translateX ( -1280px) ;
}
55% {
transform : translateX ( -1920px) ;
}
75% {
transform : translateX ( -1920px) ;
}
80% {
transform : translateX ( -2560px) ;
}
100% {
transform : translateX ( -2560px) ;
}
}
</ style>
</ head>
< body>
< div class = " swiper-container" >
< div class = " swiper-wrapper" >
< div class = " swiper-slide" >
< img src = " img/1.jpg" alt = " " >
</ div>
< div class = " swiper-slide" >
< img src = " img/2.jpg" alt = " " >
</ div>
< div class = " swiper-slide" >
< img src = " img/3.jpg" alt = " " >
</ div>
< div class = " swiper-slide" >
< img src = " img/4.jpg" alt = " " >
</ div>
< div class = " swiper-slide" >
< img src = " img/1.jpg" alt = " " >
</ div>
</ div>
</ div>
</ body>
4.逐帧动画案例
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
div {
width : 180px;
height : 300px;
background-image : url ( ./charector.png) ;
animation : run 1s steps ( 1, end) infinite;
}
@keyframes run {
0% {
background-position : 0px 0;
}
14.3% {
background-position : -180px 0;
}
28.6% {
background-position : -360px 0;
}
42.9% {
background-position : -540px 0;
}
57.2% {
background-position : -720px 0;
}
71.5% {
background-position : -900px 0;
}
85.8% {
background-position : -1080px 0;
}
100% {
background-position : -1260px 0;
}
}
</ style>
</ head>
< body>
< div> </ div>
</ body>
5.Animate动画库
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< link rel = " stylesheet" href = " https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.1/animate.min.css" >
< style>
div {
width : 200px;
height : 200px;
background : red;
margin : 0 auto;
}
.kerwininfinite {
animation-iteration-count : 3;
animation-duration : 3s;
}
</ style>
</ head>
< body>
< div class = " animated wobble kerwininfinite" > </ div>
</ body>
6.3D平移
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
.box {
width : 500px;
height : 500px;
border : 5px solid black;
transform-style : preserve-3d;
position : relative;
margin : 0 auto;
perspective : 900px;
}
.center {
position : absolute;
width : 200px;
height : 200px;
background : red;
left : 50%;
top : 50%;
margin-left : -100px;
margin-top : -100px;
transition : all 5s;
}
.box:hover .center {
transform : translate3d ( 0, 0, 200px) ;
}
</ style>
</ head>
< body>
< div class = " box" >
< div class = " center" > </ div>
</ div>
</ body>
7.3D旋转
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
.box {
width : 500px;
height : 500px;
border : 5px solid gray;
display : flex;
transform-style : preserve-3d;
}
.center {
margin : auto;
width : 200px;
height : 200px;
background : red;
transform : rotate3d ( 0, 0, 1, 30deg) ;
}
</ style>
</ head>
< body>
< div class = " box" >
< div class = " center" > </ div>
</ div>
</ body>
8.3D缩放
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
.box {
width : 500px;
height : 500px;
border : 5px solid gray;
display : flex;
transform-style : preserve-3d;
perspective : 800px;
}
.center {
margin : auto;
width : 200px;
height : 200px;
background : red;
transform : scale3d ( 1, 1, 10) rotateX ( 45deg) ;
}
</ style>
</ head>
< body>
< div class = " box" >
< div class = " center" > </ div>
</ div>
</ body>
9.立方体案例
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
* {
margin : 0;
padding : 0;
}
.box {
width : 600px;
height : 600px;
margin : 0 auto;
border : 5px solid gray;
transform-style : preserve-3d;
position : relative;
transform : rotateY ( 30deg) rotateX ( 30deg) ;
animation : run 5s linear infinite;
}
@keyframes run {
0% {
transform : rotateY ( 30deg) rotateX ( 30deg) ;
}
50% {
transform : rotateY ( 300deg) rotateX ( 300deg) ;
}
100% {
transform : rotateY ( 30deg) rotateX ( 30deg) ;
}
}
.box div {
width : 200px;
height : 200px;
position : absolute;
left : 50%;
top : 50%;
margin-left : -100px;
margin-top : -100px;
line-height : 200px;
text-align : center;
font-size : 50px;
color : white;
opacity : 0.7;
}
.box div:nth-child(1) {
background : gray;
transform : translateZ ( 100px) ;
}
.box div:nth-child(2) {
background : cadetblue;
transform : translateX ( -100px) rotateY ( -90deg) ;
}
.box div:nth-child(3) {
background : purple;
transform : translateY ( -100px) rotateX ( 90deg) ;
}
.box div:nth-child(4) {
background : green;
transform : translateY ( 100px) rotateX ( -90deg) ;
}
.box div:nth-child(5) {
background : red;
transform : translateX ( 100px) rotateY ( 90deg) ;
}
.box div:nth-child(6) {
background : darkblue;
transform : translateZ ( -100px) ;
}
</ style>
</ head>
< body>
< div class = " box" >
< div> 1</ div>
< div> 2</ div>
< div> 3</ div>
< div> 4</ div>
< div> 5</ div>
< div> 6</ div>
</ div>
</ body>
10.网格布局
1.使用网格布局
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
.box {
width : 200px;
height : 200px;
background : red;
display : grid;
}
</ style>
</ head>
< body>
< span> 1111</ span>
< span class = " box" > 222</ span>
< span> 3333</ span>
</ body>
2.行与列的划分
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
div.box {
width : 600px;
height : 600px;
border : 5px solid gray;
display : grid;
grid-template-rows : 100px 200px auto;
grid-template-columns : 100px 200px auto;
}
</ style>
</ head>
< body>
< div class = " box" >
< div> 1</ div>
< div> 2</ div>
< div> 3</ div>
< div> 4</ div>
< div> 5</ div>
< div> 6</ div>
< div> 7</ div>
< div> 8</ div>
< div> 9</ div>
</ div>
</ body>
3.列间距
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
.box {
width : 600px;
height : 600px;
border : 5px solid gray;
display : grid;
grid-template-rows : 200px 180px 180px;
grid-template-columns : 200px 180px 180px;
gap : 20px 20px;
}
.box div {
border : 1px solid red;
}
</ style>
</ head>
< body>
< div class = " box" >
< div> 1</ div>
< div> 2</ div>
< div> 3</ div>
< div> 4</ div>
< div> 5</ div>
< div> 6</ div>
< div> 7</ div>
< div> 8</ div>
< div> 9</ div>
</ div>
</ body>
4.指定区域(命名与合并)
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
.box {
width : 600px;
height : 600px;
display : grid;
border : 5px solid gray;
grid-template-columns : 200px 200px 200px;
grid-template-rows : 200px 200px 200px;
grid-template-areas : 'a e e'
'd e e'
'g h i'
;
}
.box div:nth-child(2) {
grid-area : e;
}
</ style>
</ head>
< body>
< div class = " box" >
< div > 1</ div>
< div> 2</ div>
< div> 3</ div>
< div> 4</ div>
< div> 5</ div>
< div> 6</ div>
</ div>
</ body>
5.网格布局案例
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
.box {
width : 650px;
height : 320px;
border : 5px solid red;
margin : 0 auto;
display : grid;
grid-template-rows : repeat ( 3, 100px) ;
grid-template-columns : repeat ( 6, 100px) ;
grid-template-areas : 'a a a a b b'
'a a a a c c'
'd d e f c c'
;
grid-gap : 10px 10px;
}
.box img {
width : 100%;
}
.box img:nth-child(1) {
grid-area : a;
}
.box img:nth-child(2) {
grid-area : d;
}
.box img:nth-child(3) {
grid-area : e;
}
.box img:nth-child(4) {
grid-area : f;
}
.box img:nth-child(5) {
grid-area : b;
}
.box img:nth-child(6) {
grid-area : c;
}
</ style>
</ head>
< body>
< div class = " box" >
< img src = " images/a1.png" alt = " " >
< img src = " images/a2.png" alt = " " >
< img src = " images/a3.png" alt = " " >
< img src = " images/a4.png" alt = " " >
< img src = " images/a5.png" alt = " " >
< img src = " images/a6.png" alt = " " >
</ div>
</ body>
6.网格布局–对齐方式
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
.box {
width : 600px;
height : 600px;
border : 5px solid gray;
display : grid;
grid-template-rows : repeat ( 3, 100px) ;
grid-template-columns : repeat ( 3, 100px) ;
place-content : center center;
place-items : center center;
}
.box div {
border : 1px solid green;
width : 50px;
height : 50px;
}
</ style>
</ head>
< body>
< div class = " box" >
< div> 1</ div>
< div> 2</ div>
< div> 3</ div>
< div> 4</ div>
< div> 5</ div>
< div> 6</ div>
< div> 7</ div>
< div> 8</ div>
< div> 9</ div>
</ div>
</ body>
7.网格线合并
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
.box {
width : 600px;
height : 600px;
display : grid;
border : 5px solid gray;
grid-template-columns : 200px 200px 200px;
grid-template-rows : 200px 200px 200px;
}
.box div:nth-child(2) {
grid-column : 2/4;
grid-row : 1/3;
}
</ style>
</ head>
< body>
< div class = " box" >
< div> 1</ div>
< div> 2</ div>
< div> 3</ div>
< div> 4</ div>
< div> 5</ div>
< div> 6</ div>
< div> 7</ div>
< div> 8</ div>
< div> 9</ div>
</ div>
</ body>
8.网格线合并案例
< head>
< meta charset = " UTF-8" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< style>
.box {
width : 650px;
height : 320px;
border : 5px solid red;
margin : 0 auto;
display : grid;
grid-template-rows : repeat ( 3, 100px) ;
grid-template-columns : repeat ( 6, 100px) ;
grid-template-areas : 'a a a a b b'
'a a a a c c'
'd d e f c c'
;
grid-gap : 10px 10px;
}
.box img {
width : 100%;
}
.box img:nth-child(1) {
grid-column : 1/5;
grid-row : 1/3;
}
.box img:nth-child(2) {
grid-column : 1/3;
grid-row : 3/4;
}
.box img:nth-child(3) {
grid-column : 3/4;
grid-row : 3/4;
}
.box img:nth-child(4) {
grid-column : 4/5;
grid-row : 3/4;
}
.box img:nth-child(5) {
grid-column : 5/7;
grid-row : 1/2;
}
.box img:nth-child(6) {
grid-column : 5/7;
grid-row : 2/4;
}
</ style>
</ head>
< body>
< div class = " box" >
< img src = " images/a1.png" alt = " " >
< img src = " images/a2.png" alt = " " >
< img src = " images/a3.png" alt = " " >
< img src = " images/a4.png" alt = " " >
< img src = " images/a5.png" alt = " " >
< img src = " images/a6.png" alt = " " >
</ div>
</ body>