本文章介紹了CSS3之transition實現下劃線的示例代碼,分享給大家,具體如下:
在這里先看看我們的demo
認識transition
這是CSS3中新增的一個樣式,可以實現動畫的過度。通常使用在添加某種效果可以從一種樣式轉變到另一個的時候。
transition屬性
transition: width 1s linear 2s; /*簡寫實例*/
/*等同如下*/transition-property: width;transition-duration: 1s;transition-timing-function: linear;transition-delay: 2s;
tranform屬性
實現我們需要的效果
當然,在這就直接放出代碼,代碼中有注釋方便理解
/*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基礎。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答