需用 transition-timing-function 来规定过渡曲线效果
贝塞尔曲线制作网站
linear 规定以相同速度开始至结束的过渡效果(等于 cubic-bezier(0,0,1,1))。 ease 规定慢速开始,然后变快,然后慢速结束的过渡效果(cubic-bezier(0.25,0.1,0.25,1))。 ease-in 规定以慢速开始的过渡效果(等于 cubic-bezier(0.42,0,1,1))。 ease-out 规定以慢速结束的过渡效果(等于 cubic-bezier(0,0,0.58,1))。 ease-in-out 规定以慢速开始和结束的过渡效果(等于 cubic-bezier(0.42,0,0.58,1))。 cubic-bezier(n,n,n,n) 在 cubic-bezier 函数中定义自己的值。可能的值是 0 至 1 之间的数值 如:transition-timing-function: cubic-bezier(1,-0.87,0,1.59);classList 属性
语法 element.classList
下面是代码部分
HTML
<div class="text-center"> <h1 id="text" >前端学院<span id="underline"></span></h1> <button class="btn" onclick="changeStyle()">切换样式</button> </div>CSS
h1{ transition: color 0.2s linear; -moz-transition: color 0.2s linear; /* Firefox 4 */ -webkit-transition: color 0.2s linear; /* Safari 和 Chrome */ -o-transition: color 0.2s linear; /* Opera */ } span{ width: 0; display: block; /*边框粗细颜色*/ border-bottom: 4px #1C86EE solid; transition:width 0.5s; margin: 0 auto; } .text{ /*过度完成后的文本颜色*/ color:#1C86EE; } .text-center{ text-align:center; } .underline{ /*下划线长度*/ width:140px; } .btn{ margin-top: 60px; outline-style:none; background-color:white; border-style:solid; border-width:2px; border-radius:10px; font-size:26px; }JavaScript
var changed = false; function changeStyle(){ if (changed == false) { document.getElementById("underline").classList.add('underline'); document.getElementById("text").classList.add('text'); changed = true; }else{ document.getElementById("underline").classList.remove('underline'); document.getElementById("text").classList.remove('text'); changed = false; } }