
cubic-bezier即貝茲曲線的繪製方法。圖上有四個點P0~P3,其中P0、P3是默認的固定點,對應[0,0]與[1,1];剩下的P1、P2兩點則由我們透過cubic-bezier()自定義。cubic-bezier(x1, y1, x2, y2)即為自定義,其中x1、x2、y1、y2的取值範圍都在[0, 1]之間。
幾個預設的特效:
ease: cubic-bezier(0.25, 0.1, 0.25, 1.0)
linear: cubic-bezier(0.0, 0.0, 1.0, 1.0)
ease-in: cubic-bezier(0.42, 0, 1.0, 1.0)
ease-out: cubic-bezier(0, 0, 0.58, 1.0)
ease-in-out: cubic-bezier(0.42, 0, 0.58, 1.0)
也就是說,第四個值y2與x2共同決定了P2的位置。
它的主要作用是在製作CSS動畫時,定義動畫各個階段的速度,作為animation屬性的取值。
實例代碼:
<style>
div.hovertreemusic {
width: 45px;
height: 45px;
animation: hovertreerotate 2s cubic-bezier(0.42, 0, 0.58, 1.0) infinite;
-webkit-animation: hovertreerotate 2s linear cubic-bezier(0.42, 0, 0.58, 1.0); /*Safari and Chrome*/
margin: 2px;
background-image: url(https://hovertree.com/wx/hwq/10/css/music_note_big.png);
}
div.myhovertree {
width: 45px;
height: 45px;
animation: hovertreerotate 2s cubic-bezier(0.15, 0, 0.28, 1.0) infinite;
-webkit-animation: hovertreerotate 2s linear cubic-bezier(0.42, 0, 0.58, 1.0); /*Safari and Chrome*/
margin: 2px;
background-image: url(https://hovertree.com/wx/hwq/10/css/music_note_big.png);
}
div.hwq2 {
width: 45px;
height: 45px;
animation: hovertreerotate 2s cubic-bezier(0.65, 0.5, 0.68, 0.8) infinite;
-webkit-animation: hovertreerotate 2s linear cubic-bezier(0.42, 0, 0.58, 1.0); /*Safari and Chrome*/
margin: 2px;
background-image: url(https://hovertree.com/wx/hwq/10/css/music_note_big.png);
}
@keyframes hovertreerotate {
from {
margin-left:10px;
}
to {
margin-left:400px;
}
}
@-webkit-keyframes hovertreerotate /*Safari and Chrome*/
{
from {
margin-left:10px;
}
to {
margin-left:400px;
} }
</style><div class="hovertreemusic"></div>
<div class="myhovertree"></div>
<div class="hwq2"></div>
效果: