CSS中实现动画效果-附案例
⼀、什么是动画?
动画就是⼀帧⼜⼀帧图⽚,按顺序展现在⼈的眼前,但是由于⼈的视觉反应不过来就会产⽣图画动起来的效果。⼆、动画动作
1.动画的声明@keyframes name
动画声明需要使⽤@keyframes name,后⾯的name是⼈为定义的动画名称
@keyframes move {
0% {
transform: translate(0, 0);
}
25% {
transform: translate(1000px, 0px);
}
50% {
transform: translate(1000px, 400px);
}
75% {
transform: translate(0px, 400px);
}
100% {
transform: translate(0, 0);
}
}
2.动画的动作
在以上代码中,{}内包含的就是动画的动作,每⼀个完整的动画都会⼀定的时间,其中
意思是每达到时间的百分之多少就向某状态进⾏转变。
三、动画属性
<!--
动画属性有很多种
div {
动画的名称(必须要有)
animation-name: move;
动画的运动曲线(linear是匀速stept()是分步)
animation-timing-function: linear;
动画的运动时间
animation-duration: 3s;
动画的运动次数(infinite是⽆限循环)
animation-iteration-count: infinite;
动画的开始时间
animation-delay: 1s;
动画是否逆序播放
animation-direction: alternate;
}
动画播放期间触碰暂停
div:hover {
animation-play-state: paused;
}
动画播放完毕是否回到初始位置
animation-fill-mode: forwards;(不回到初始位置)
backwards(回到初始位置)
-->
1.代码⽰例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title>
<style>
@keyframes move {
0% {
width: 0;
height: 0;
}
100% {
width: 300px;
height: 300px;
}
}
div {
margin: 200px auto;
/* width: 300px;
height: 300px; */
background-color: darkviolet;
animation-name: move;
animation-timing-function: linear;            animation-duration: .4s;
animation-iteration-count: infinite;            animation-delay: 1s;
animation-direction: alternate;
animation-fill-mode: forwards;
}
div:hover {
animation-play-state: paused;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
2.效果展⽰
四、项⽬案例
1.奔跑的⼩熊
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
css实现垂直水平居中
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title>
<!--
每⼀帧图⽚连接到⼀块就组成了⼀个动画
-
->
<style>
body {
position: relative;
background-color: gray;
}
@keyframes bear {
0% {
background-position: 0px 0px;
}
100% {
background-position: -1600px 0px;
}
}
@keyframes bgcmove {
0% {
left: 0;
}
100% {
left: 50%;
transform: translateX(-50%);
}
}
@keyframes mountains {
0% {
background-position: 0px 0px;
}
100% {
background-position: -3840px 0px;
}
}
.mountain {
/*
这⾥定位定到⽗类的底部,得不到想要的结果
*/
position: absolute;
bottom: -601px;
width: 100%;
height: 200px;
background-image: url(../bg1.png);
animation: mountains 8s linear infinite;
}
.nav {
position: absolute;
bottom: -601px;
width: 200px;
height: 100px;
background-image: url(../bear.png);
/* background-size: 100% 100%; */
/* animation: name duration timing-function delay iteration-count direction fill-mode; */            /* 值得注意的是steps与linear不可以混合使⽤ */
animation: bear .8s steps(8) infinite, bgcmove 3s linear forwards;
}
</style>
</head>
<body>
<div class="mountain"></div>
<div class="nav"></div>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。