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