css动画
css动画
过渡
-
过渡 (transition)的作用
1.通过过渡可以指定一个属性发生变化时的切换方式
2.通过过渡可以创建一些非常好的效果,提升用户的体验 -
常用属性
1.transition-property:
指定要执行过渡的属性
多个属性间使用,
隔开( transition-property: width,height;)
如果所有属性都需要过渡,则使用all关键字
大部分属性都支持过渡效果,注意过渡时必须是有效数值向另外一个有效数值进行过渡,比如auto向0过渡就不是一个有效值2.transition-duration:
指定过渡效果的持续时间
时间单位:s 和 ms 1s=1000ms
可以分别指定时间(transition-duration: 100ms,2s;)3.transition-timing-function:
过渡的时序函数
指定过渡的执行的方式
可选值:
ease 默认值 ,慢速开始,先加速,再减速实例
linear 匀速运动
ease-in加速运动
减速运动ease-out
先加速在减速ease-in-out
cubic-bezier(0,.5,.97,-0.31)用贝塞尔曲线来指定时序函数
分步steps(3)可以设置一个第二个值,end在时间结束时执行过渡(默认值)例:steps(3,end),start在时间开始时执行过程
4. transition-delay:
过渡效果的延迟,等待一段时间后在执行过渡
transition-delay:2s ;
5.transition:
可以同时设置过度相关的所有属性,只有一个要求,如果要写延迟,则两个时间中第一个是持续时间,第二个是延迟。
<!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>
*{
margin: 0;
padding: 0;
}
.box1{
width: 800px;
height: 400px;
background-color: silver;
margin-left: 0;
}
.box1 div{
width: 100px;
height: 100px;
margin-bottom: 100px;
}
.box2{
background-color: red;
/* transition: all 2s; */
transition-property: all;
transition-duration: 2s;
/* transition-timing-function:cubic-bezier(0,.5,.97,-0.31) ; */
/* transition-timing-function: steps(3,end); */
/* transition-delay:2s ; */
transition: margin-left 2s 1s;
}
.box3{
background-color: orange;
transition-property: all;
transition-duration: 2s;
transition-timing-function: ease;
}
.box1:hover div{
/* width: 200px; */
/* height: 200px; */
/* background-color: orange; */
margin-left: 700px;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
<div class="box3"></div>
</div>
</body>
</html>
动画
-
动画的简介
1.动画和过渡类似,都是可以实现一些动态的效果,不同的是过渡需要在某个属性发生变化时才会触发动画可以自动触发动态效果
2.设置动画效果,必须先要设置一个关键帧,关键帧设置了动画执行每一个步骤 -
设置关键帧
/*@keyframes后跟的是关键帧的名字(可以随便起)*/
@keyframes text {
/*from表示动画的开始位置 也可以使用0%*/
from{
margin-left: 0;
}
/*to动画的结束位置 也可以使用100%*/
to{
margin-left: 700px;
}
}
-
动画常用属性
1.animation-name:
指定要对当前元素生效的关键帧的名字2.animation-duration:
指定动画的执行时间3.animation-timing-function:
跟transition-timing-function用法一样4. animation-iteration-count:
指定动画执行的次数 animation-iteration-count:infinite;(无限执行)5.animation-direction:
指定动画运行的方向
normal 默认值 从from向to运行 每次都是这样
reverse 从 to 向 from 运行 每次都是这样
alternate 从 from 向 to 运行 重复执行动画时反向执行
alternate-reverse 从 to 向 from 运行 重复执行动画时反向执行6. animation-play-state:
设置动画的执行状态
可选值:
running 默认值 动画执行
paused 动画暂停7.animation-fill-mode:
指定动画的填充模式
可选值:
none 默认值 动画执行完毕元素回到原来位置
forwards 动画执行完毕元素会停止在动画结束的位置
backwards 动画延时等待时,元素就会处于开始位置
both 结合了 forwards 和 backwards8.animation-delay:
设置动画的延时8.transition:
可以同时设置动画相关的所有属性,只有一个要求,如果要写延迟,则两个时间中第一个是持续时间,第二个是延迟。
sprite animation(百度搜索可以得到动画素材)
实例
素材:
代码:
<!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>
.box1{
background-image: url(./image/雪人.jpg);
height:180px;
width: 123px;
margin: 0 auto;
animation-name: run;
animation-duration: 2s;
animation-iteration-count: infinite;
animation-timing-function: steps(4);
}
@keyframes run{
from{
background-position: 0 0;
}
to{
background-position: -487px 0;
}
}
</style>
</head>
<body>
<div class="box1"></div>
</body>
</html>
实例:
代码:
<!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>
.outer{
height: 500px;
border-bottom: 10px black solid;
margin: 50px auto;
/* 解决外边距重叠 */
overflow: hidden;
}
.outer div{
float: left;
width: 100px;
height: 100px;
border-radius: 50%;
background-color: red;
animation: ball .3s forwards linear infinite alternate;
}
div.box2{
background-color: orange;
animation-delay: .1s;
}
div.box3{
background-color: yellow;
animation-delay: .2s;
}
div.box4{
background-color: yellowgreen;
animation-delay: .3s;
}
div.box5{
background-color: blueviolet;
animation-delay: .4s;
}
div.box6{
background-color: pink;
animation-delay: .5s;
}
div.box7{
background-color: skyblue;
animation-delay: .6s;
}
div.box8{
background-color: chocolate;
animation-delay: .7s;
}
div.box9{
background-color: blue;
animation-delay: .8s;
}
/* 小球下落的动画 */
@keyframes ball{
from{
margin-top: 0;
}
to{
margin-top: 400px;
}
}
</style>
</head>
<body>
<div class="outer">
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
<div class="box5"></div>
<div class="box6"></div>
<div class="box7"></div>
<div class="box8"></div>
<div class="box9"></div>
</div>
</body>
</html>
变形
-
变形(transform)的简介
1.变形就是指通过css来改变元素的形状或位置
2.变形不会影响到页面的布局(不会脱离文档流)
3.平移元素,百分比是相对于自身计算的 -
常用属性(transform)
1.transform: translateX():
沿着x轴平移;
2.transform: translateY():
沿着Y轴平移;
3.transform: translateX():
沿着Z轴平移;
1.Z轴平移,调整元素在Z轴的位置,正常情况就是调整元素和人眼之间的距离,距离越大,元素离人越近
2.Z轴平移属于立体效果(近大远小),默认情况下网页是不支持透视,如果需要看见效果必须要设置网页的视距
Z轴平移实例
<!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>
html{
/* 设置当前网页的视距为800px,人眼距离网页的距离 */
perspective: 800px;
}
.box1{
width: 400px;
height: 400px;
margin: 0 auto;
margin-top: 200px;
background-color: silver;
}
.box2{
width: 150px;
height: 200px;
background-color:skyblue;
margin: 0 auto;
}
.box2:hover{
transform: translateZ(200px);
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
实例:
代码:
<!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>
*{
background-color: rgb(155, 148, 148);
}
/* .box1{
width: 200px;
height: 200px;
background-color: #bfa;
margin: 0 auto;
margin-top: 200px;
/* transform用来设置元素的变形效果 */
/* transform: translateX();沿着x轴平移 */
/* transform: translateY();沿着y轴平移 */
/* transform: translateZ();养着z轴平移 */
/* transform: translateZ(-50%) translateY(-50%);
} */
.box2{
width: 150px;
height: 200px;
background-color:skyblue;
margin: 0 auto;
margin-top: 200px;
transition: transform , .3s;
}
.box2:hover{
transform: translateY(-10px);
box-shadow:0 0 10px rgba(0, 0, 0, .3);
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
旋转
-
旋转(transform)的简介
1.通过旋转可以使用元素沿着x y 或 z旋转指定的角度
-
(transform)常用属性
1.rotateX()
沿着x轴旋转;
2.rotateY()
沿着Y轴旋转;
3.rotateZ()
沿着Z轴旋转;
实例:
<!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>
html{
perspective: 800px;
}
.box1{
width: 200px;
height: 200px;
margin: 0 auto;
margin-top: 200px;
background-color: silver;
/* transform: rotateX();
transform: rotateY();
transform: rotateZ(); */
transition: all 2s;
}
/* .box2{
width: 150px;
height: 200px;
background-color:skyblue;
margin: 0 auto;
} */
.box1:hover{
transform: rotateY(180deg) translateZ(400px);
}
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2"></div>
</body>
</html>
实例:
<!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>
html{
perspective: 800px;
}
.box1{
width: 200px;
height: 200px;
margin: 0 auto;
margin-top: 200px;
background-color: silver;
/* transform: rotateX();
transform: rotateY();
transform: rotateZ(); */
transition: all 2s;
}
/* .box2{
width: 150px;
height: 200px;
background-color:skyblue;
margin: 0 auto;
} */
.box1:hover{
/* transform: rotateY(180deg) translateZ(400px); */
transform: translateZ(400px) rotateY(180deg);
}
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2"></div>
</body>
</html>
时钟实例
素材:
效果:
代码:
<!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>
*{
margin: 0;
padding: 0;
}
/* 设置表的样式 */
.clock{
width: 500px;
height: 500px;
margin: 0 auto;
border-radius: 50%;
/* border: 10px solid black; */
position: relative;
background-image: url(./image/表盘.jpg);
background-size:cover;
background-position:center;
}
.clock > div{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
}
/* 设置时针 */
.hour-wrapper{
height: 40%;
width: 40%;
animation: run 7200s linear infinite;
}
.hour{
height: 50%;
width: 6px;
background-color: black;
margin: 0 auto;
}
/* 设置分针 */
.min-wrapper{
height: 60%;
width: 60%;
animation: run 600s steps(60) infinite;
}
.min{
height: 50%;
width:4px;
background-color: black;
margin: 0 auto;
}
/* 设置秒针 */
.sec-wrapper{
height: 80%;
width: 80%;
animation: run 10s steps(60) infinite;
}
.sec{
height: 50%;
width:2px;
background-color: red;
margin: 0 auto;
}
/*
旋转的关键帧
*/
@keyframes run{
from{
transform: rotateZ(0deg);
}
to{
transform: rotateZ(360deg);
}
}
</style>
</head>
<body>
<!-- 创建表的容器 -->
<div class="clock">
<!-- 创建时针-->
<div class="hour-wrapper">
<div class="hour"></div>
</div>
<!-- 创建分针-->
<div class="min-wrapper">
<div class="min"></div>
</div>
<!-- 创建秒针-->
<div class="sec-wrapper">
<div class="sec"></div>
</div>
</div>
</body>
</html>
立方体实例
用到的属性:
/* 设置当前网页的视距为800px,人眼距离网页的距离 */
perspective: 800px;
/* transform默认情况下是2d变形 如果想要设置3d变形效果需要设置 transform-style: preserve-3d;*/
transform-style: preserve-3d;
代码:
<!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>
html{
/* 设置当前网页的视距为800px,人眼距离网页的距离 */
perspective: 800px;
}
.cube{
width: 400px;
height: 400px;
margin: 150px auto;
/* transform默认情况下是2d变形 如果想要设置3d变形效果需要设置 transform-style: preserve-3d;*/
transform-style: preserve-3d;
animation: rotate 3s linear infinite;
}
.cube > div{
opacity: .8;
position: absolute;
}
img{
width: 400px;
height: 400px;
vertical-align: top;
}
.box1{
transform: rotateY(90deg) translateZ(200px);
}
.box2{
transform: rotateY(-90deg) translateZ(200px);
}
.box3{
transform: rotateX(90deg) translateZ(200px);
}
.box4{
transform: rotateX(-90deg) translateZ(200px);
}
.box5{
transform: rotateX(0deg) translateZ(-200px);
}
.box6{
transform:translateZ(200px) rotateY(180deg) ;
}
@keyframes rotate{
from{
transform: rotateX(0) rotateZ(0);
}
to{
/* turn表示转一圈 */
transform: rotateX(1turn) rotateZ(1turn);
}
}
</style>
</head>
<body>
<!-- 创建一个外部的容器 -->
<div class="cube">
<!-- 引入图片 -->
<div class="box1">
<img src="./beauty_image/2.jpg" alt="">
</div>
<div class="box2">
<img src="./beauty_image/3.jpg" alt="">
</div>
<div class="box3">
<img src="./beauty_image/4.jpg" alt="">
</div>
<div class="box4">
<img src="./beauty_image/5.jpg" alt="">
</div>
<div class="box5">
<img src="./beauty_image/6.jpg" alt="">
</div>
<div class="box6">
<img src="./beauty_image/7.jpg" alt="">
</div>
</div>
</body>
</html>
缩放
-
(transform)常用属性
transform-origin: 0 0;
设置变形的原点 默认值(center)
1.scaleX(2)
沿着X轴放大两倍;
2.scaleX(.2)
沿着X轴缩小两倍;
3.scaleY(2)
沿着Y轴放大两倍;
4.scaleY(.2)
沿着Y轴缩小两倍;
4.scale(2)
沿着Y轴和X轴放大两倍;
4.scale(.2)
沿着Y轴和X轴缩小两倍;
图片放大实例:
代码:
<!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>
.box1{
width: 100px;
height: 100px;
background-color: #bfa;
transition: 2s;
margin: 100px auto;
}
.box1:hover{
transform: scale(2);
}
.img-wrapper{
margin: 100px auto;
width: 200px;
height: 200px;
/* border: 1px red solid; */
overflow: hidden;
}
img{
transition: .8s;
}
.img-wrapper:hover img{
transform: scale(1.2);
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="img-wrapper">
<img src="./beauty_image/3.jpg" width="100%" height="100%">
</div>
</body>
</html>