觸發(fā)方式有:1、通過偽類元素“:hover”觸發(fā),語法“元素{transition:屬性 過渡時間}元素:hover{屬性:屬性值}”;2、通過“element.classList.add("元素名稱")”語句觸發(fā)css過渡效果。
本教程操作環(huán)境:windows7系統(tǒng)、CSS3&&HTML5版、Dell G3電腦。
第一種: 通過偽類元素觸發(fā)
<style> .box{ width: 100px; height: 100px; background-color: blueviolet; transition: width 1s linear .5s; } .box:hover{ width: 400px; } </style> <p class="box"> </p>
第二種: 通過JS觸發(fā)
通過js添加必須有一定的延遲(延遲去掉無效果)來改變元素的樣式
<style> .box{ width: 100px; height: 100px; background-color: blueviolet; transition: width 1s linear .5s; } .box1{ width: 400px; }</style> <p class="box"> </p> <scrpit> setTimeout(() => { let element = document.getElementsByClassName('box')[0]; element.classList.add('box1') }, 1) </scrpit>
推薦學習:css視頻教程