动画效果与过渡效果有何不同呢?
过渡效果是一个CSS样式到另一个CSS样式。
动画效果比过渡效果更复杂。
一、初入门案例
<!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>Demo14</title>
<style>
@keyframes anim {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.box {
background-color: red;
height: 100px;
width: 100px;
margin: 100px auto;
animation: anim 2s;
}
</style>
</head>
<body>
<div class="box">
hello
</div>
</body>
</html>
效果如下:
页面刷新时,div旋转,从0°旋转到360°,在2秒内完成。
@keyframes用于定义一个动画
二、动画属性(animation)
animation-name:规定需要绑定到选择器的keyframe名称。
animation-duration:规定完成动画所花费的时间,以秒或毫秒计。
animation-timing-function:规定动画的速度曲线。
animation-delay:规定在动画开始之前的延迟。
animation-iteration-count:规定动画应该播放的次数。
设置多个动画属性
animation: anim 2s linear 1s infinite;
linear:匀速变化
三、案例(一)——CD旋转效果
<!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>Demo15</title>
<style>
.box {
width: 200px;
height: 200px;
margin: 100px auto;
border-radius: 50%;
overflow: hidden;
animation: anim 3s linear infinite;
}
.box img {
width: 200px;
height: 200px;
}
@keyframes anim {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.box:hover {
animation-play-state: paused;
}
</style>
</head>
<body>
<div class="box">
<img src="../picture/稻香.jpg" alt="">
</div>
</body>
</html>
效果如下:
鼠标悬浮时,暂停旋转。