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>
*{
margin: 0;
padding: 0;
}
.box1{
width: 800px;
height: 800px;
background-color: silver;
overflow: hidden;
}
.box1 div{
width: 100px;
height: 100px;
margin-bottom: 100px;
}
.box2{
background-color: #bfa;
transition: height 2s;
/*
过渡(transition)
-通过过渡可以指定一个属性发生变化时的切换方式
-通过过渡可以创建一些非常好的效果,提升用户体验
*/
/* transition-property: ; 指定执行过渡的属性
多个属性间使用,隔开
如果所有属性都需要过渡,则使用all关键字
大部分属性都支持过渡效果,注意过渡时必须是从一个有效数值向另外一个有效数值进行过渡
transition-duration: ;指定过渡效果的持续时间
时间单位:s ms 1s=1000ms
transition-timing-function: ;过渡的时序函数,指定过度的执行方式
可选值:
ease 默认值 慢速开始,先加速再减速
linner 匀速运动
ease-in 慢速开始 加速运动
ease-out 减速运动
ease-in-out 先加速 后减速
cubic-bezier()指定时序函数
https://cubic-bezier.com
step()分布执行过渡效果
可以设置一个第二个值
end 在时间结束时执行过渡(默认值)
start 在时间开始时执行过渡
transition-delay: ; 过渡效果延迟,等待一段时间后在执行过渡
trabsition 可以同时设置过度相关的所有属性,只有一个要求,如果要写延迟,则两个时间第一个是持续时间 后一个是延迟时间
*/
}
.box1:hover .box2{
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
动画(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>
*{
margin: 0;
padding: 0;
}
.box1{
width: 800px;
height: 800px;
background-color: silver;
overflow: hidden;
}
.box1 div{
width: 100px;
height: 100px;
margin-bottom: 100px;
margin-left: 0;
}
.box2{
background-color: #bfa;
/* animation-name: ; 要对当前元素生效的关键帧的名字 */
animation-name: test;
/* animation-duration: ;动画的执行时间 */
animation-duration: 4s;
/* animation-delay: ; 动画的延时 */
animation-delay: 2s;
/* animation-timing-function: ; */
animation-timing-function: ease-in-out ;
/* animation-iteration-count: 动画执行的次数
可选值:
次数
infinite 无限执行
*/
animation-iteration-count: 1;
/* animation-direction 指定动画运行的方向
normal 默认值 从from到to
reverse 从to到from运行
alternate 从from向to运行 重复执行时动画反向执行
alternate-reverse 从to 向from运行 重复执行动画时反向执行
*/
animation-direction: normal;
/* animation-play-state: ; 设置动画的执行状态
可选值
running 默认值 动画执行
pasued 动画暂停 */
animation-play-state: paused;
/* animation-fill-mode: ;动画的填充模式
可选值
none默认值 动画执行完毕元素回到原来位置
forwards 动画执行完毕元素会停止在动画结束的位置
backwards 动画延时等待,元素就会处于开始位置
both 结合了forwads 和backwards
*/
animation-fill-mode: none;
}
.box1:hover div{
margin-left: 700px;
}
/*
动画,和过渡类似,都是可以实现一些动态的效果
不同的是过渡需要在某个属性发生变化时才会触发
动画可以自发触动动态效果
设置动画效果,必须要设置一个关键帧,关键帧设置了动画执行的每一个步骤
*/
@keyframes test{
/* from表示动画开始位置 ,也可以使用0%*/
from{
margin-left: 0;
}
/* to表示动画结束位置,也可以使用100% */
to{
margin-left: 700px;
}
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
</body>
</html>
动画(2)
<!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;
}
.box1{
width: 100px;
height: 100px;
border-radius: 50%;
background-color: red;
animation: ball 1s forwards ease-in;
}
/* 创建小球下落的动画 */
@keyframes ball{
from{
margin-top: 0;
}
20%, 60%,to{
margin-top: 400px;
animation-timing-function: ease-in;
}
40%{
margin-top: 100px;
}
80%{
margin-top: 200px;
}
}
</style>
</head>
<body>
<div class="outer">
<div class="box1"></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>
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
margin: 0 auto ;
margin-top: 200px;
/*
变形就是指通过css来改变元素的形状或位置
-变形不会影响页面的布局
-transform 用来设置元素的变形效果
平移:
translateX()沿着x轴方向平移
translateY()沿着y轴方向平移
translateZ()沿着z轴方向平移
-平移元素时,百分比是相对于自身计算的
*/
transform:translate()
}
/* .box2{
width: 200px;
height: 200px;
background-color: orange;
margin: 0 auto ;
} */
.box3{
width: 100px;
height: 100px;
background-color: orange;
position: absolute;
/*
这种居中方式,只适用于元素大小确定
top:0;
left:0;
bottom:0;
right:0;
margin:auto;
*/
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(50%);
}
.box4,.box5{
width: 220px;
height: 300px;
background-color: #fff;
float: left;
margin: 0 10px;
transition: all .3s;
}
.box4:hover, .box5:hover{
transform: translateY(-4px);
box-shadow: 0 0 10px rgb(0 , 0, 0,.3);
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
<div class="box5"></div>
</body>
</html>
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;
}
body{
border: 1px red solid;
background-color: rgb(241, 241, 241);
}
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
margin: 200px auto;
/*
z轴平移,调整元素在z轴的位置,正常情况下就是调整元素和人眼之间的距离
距离越大,元素离人越近
z轴平移属于立体效果(近大远小),默认情况下网页是不支持透视的,如果需要看见效果
必须设置网页的视距
*/
transition: 2s;
}
body:hover .box1{
transform: translateZ(100px);
}
</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>
html{
/* 设置当前网页的视距为800px,人眼距离网页的距离 */
perspective: 800px;
}
body{
border: 1px red solid;
background-color: rgb(241, 241, 241);
}
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
margin: 200px auto;
transition: 2s;
}
body:hover .box1{
/*
通过旋转可以使元素沿着x y或z旋转指定的角度
rotateX()
rotateY()
rotateZ()
*/
transform: rotateZ(180deg) translateZ(100px);
transform:translateZ(100px) rotateZ(180deg) ;
}
</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>
html{
perspective: 800px;
}
.box1{
width: 100px;
height: 100px;
background-color: #bfa;
transition:2s;
margin: 100px auto;
/* 变形的原点 默认值(center)*/
transform-origin: 20px 20px;
}
.box1:hover{
/* 对元素进行缩放的函数
scaleX()水平方向缩放
scaleY()垂直方向缩放
scale()双方向缩放
*/
transform: scaleX(2);
}
.img-wrapper{
width: 200px;
height: 200px;
border: 1px red solid;
overflow: hidden;
}
img{
transition: .2s;
}
.img-wrapper:hover{
transform: scale(1.2);
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="img-wrapper">
<img src="an.jpg" width="100%">
</div>
</body>
</html>
Q.E.D.