在這裏先看看咱們的democss
這是CSS3中新增的一個樣式,能夠實現動畫的過分。一般使用在添加某種效果能夠從一種樣式轉變到另外一個的時候。html
transition-timing-function: 規定過渡效果的時間曲線。默認是 "ease"。前端
transition: width 1s linear 2s; /*簡寫實例*/ /*等同以下*/ transition-property: width; transition-duration: 1s; transition-timing-function: linear; transition-delay: 2s;
固然,在這就直接放出代碼,代碼中有註釋方便理解函數
/*css代碼*/ h2{ position: relative; padding: 15px; text-align: center; } button{ width: 100px; height: 40px; border-radius: 15px; border: none; background: #188FF7; color: #fff; outline: none; cursor: pointer; font-weight: bold; } button:hover{ background: #188EA7; } .container{ width: 600px; display: flex; flex-direction: column; align-items: center; margin: 0 auto; } .line{ position: absolute; left: 0; bottom: 0; height: 3px; width: 100%; transition: transform .5s; background: #188EA7; color: #188EA7; transform: scaleX(0); z-index: 1111; } @keyframes changeColor1{ from{ color: #000; } to{ color: #188EA7; } } @keyframes changeColor2{ from{ color: #188EA7; } to{ color: #000; } }
<!--html代碼--> <div class="container"> <h2 id="title"> 百度前端學院 <i class="line" id="line"></i> </h2> <button id="change">Change</button> </div>
//js部分代碼 (function () { let btn = document.getElementById('change'); let h2 = document.getElementById('title'); let line = document.getElementById('line'); let count = 0; btn.onclick = function () { if(count%2===0){ line.style.transform = "scaleX(1)"; h2.style.animation = "changeColor1 1s"; h2.style.color = "#188EA7"; count++; }else{ line.style.transform = "scaleX(0)"; h2.style.animation = "changeColor2 1s"; h2.style.color = "#000"; count++; } } })();
到這裏咱們就已經將此效果徹底呈現,同時咱們也學習了CSS3中的transition屬性和tranform屬性。固然完成此動畫還須要有一些html和css基礎。學習
成功不在一朝一夕間,咱們都須要努力flex