如果使用javascript是选择器,是否可以在元素的两个状态之间进行平滑过渡?例如,我有一个元素,其类为click__box,其ID为clickBox。通过使用JS单击按钮时,我可以将不透明度设置为0到1。问题在于过渡属性完全无效。我知道可以将其用作纯CSS的复选框hack,但我想知道是否可以使用JavaScript做到这一点。
//CSS
.click__box {
width:100%;
height:100%;
opacity:0;
transition:all .5s;
}
//JS
const collectionButton = document.querySelector("#collectionButton")
var clickBoxStyle = document.getElementById("clickBox").style
collectionButton.addEventListener('click', (e) => {
clickBoxStyle.opacity = "1"
})
效果很好。
const collectionButton = document.querySelector("#collectionButton")
var clickBoxStyle = document.getElementById("clickBox").style
collectionButton.addEventListener('click', (e) => {
clickBoxStyle.opacity = "1"
})
.click__box {
width: 100%;
height: 100%;
opacity: 0;
transition: all .5s;
background-color: GREEN;
}
#collectionButton {
width: 150px;
height: 50px;
background-color: red;
cursor: pointer;
}
.container {
position: relative;
width: 150px;
height: 200px;
background-color: blue;
cursor: pointer;
}
<div id="collectionButton">
collectionButton click me
</div>
<div class="container">
Click Box Container
<div id="clickBox" class="click__box">
Click Box
</div>
</div>