css3新增了transition与animation两种方式可实现动画,那么在出现transition与animation以前,有两种方式实现动画:一种是使用css的hover伪类属性方式,另一种是使用js改变css属性方式;接下来我会介绍transition与animation的使用方法以及解决了什么问题,最后会分析咱们为何使用transition与animation实现动画要比js方式性能要高效!javascript
咱们以前使用hover伪类实现动画,是如下效果:css
.div {
width:100px;
height:100px;
background:red;
}
.div:hover {
width:200px;
height:200px;
background:green;
}
复制代码
.div {
width:100px;
height:100px;
background:red;
transition:3s;
}
.div:hover {
width:200px;
height:200px;
background:green;
}
复制代码
.div {
width:100px;
height:100px;
background:red;
transition:width 3s;
}
.div:hover {
width:200px;
height:200px;
background:green;
}
复制代码
.div {
width:100px;
height:100px;
background:red;
transition:width 3s, background 3s;
}
.div:hover {
width:200px;
height:200px;
background:green;
}
复制代码
.div {
width:100px;
height:100px;
background:red;
transition:width 3s, height 2s 3s ease-in-out, background 3s 5s ease-in-out;
}
.div:hover {
width:200px;
height:200px;
background:green;
}
复制代码
animation能够实现transiton的效果,看下面代码;html
.div {
width:100px;
height:100px;
background:red;
}
.div:hover {
animation: move 3s forwards;
}
@keyframes move {
100% {
background: green;
width:200px;
height:200px;
}
}
复制代码
.div {
width:100px;
height:100px;
background:red;
animation: move 3s infinite alternate;
}
@keyframes move {
100% {
background: green;
width:200px;
height:200px;animation-name
}
}
复制代码
.div {
width:200px;
height:200px;
background:red;
margin: 100px auto;
-webkit-animation: move 3s infinite alternate;
animation: move 3s infinite alternate;
}
@keyframes move {
0% {
background: red;
transform: scale(0.4);
}
50% {
background: yellow;
border-radius: 100%;
}
100% {
background: green;
transform: scale(1.5);
}
}
复制代码
要分析动画优化,那不得不提下浏览器对页面的渲染原理;java
浏览器对页面的渲染原理,可分为如下步骤: 1.根据HTML构建DOM树;
2.根据CSS构建CSSOM树;
3.将DOM树与CSSOM树合成一个渲染树(render tree);
4.Layout布局,包括文档流、盒模型、计算大小和位置;
5.Paint绘制,包括边框颜色、文字颜色、阴影等画出来;
6.Compose合成,根据层叠关系展现画面;
css3
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style> div { width: 100px; height: 100px; background: red; float: left; } div:nth-child(2) { background: yellow; } div:nth-child(3) { background:black; } </style>
</head>
<body>
<div id="div"></div>
<div></div>
<div></div>
<script> let div = document.getElementById("div"); setInterval(()=>{ div.style.display='none' },1000) </script>
</body>
</html>
复制代码
执行代码以下:web
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
width: 100px;
height: 100px;
background: red;
float: left;
}
div:nth-child(2) {
background: yellow;
}
div:nth-child(3) {
background:black;
}
div:hover {
background:deeppink;
}
</style>
</head>
<body>
<div id="div"></div>
<div></div>
<div></div>
</body>
</html>
复制代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
#div {
width: 100px;
height: 100px;
background: red;
margin: 100px auto;
-webkit-animation: change 3s infinite alternate;
animation: change 3s infinite alternate;
}
@keyframes change {
0% {
transform: translateX(100PX);
}
50% {
transform: translateX(300PX);
}
100% {
transform: translateX(500PX);
}
}
</style>
</head>
<body>
<div id="div"></div>
</body>
</html>
复制代码
参考文档:css动画简介 animation tricks
结束语:感谢饥人谷,以上文章记录了本身在饥人谷的学习内容;chrome