纯CSS实现同级元素点击显示,无display:none或visibility
Element 1
.element1 {
background-color: #ddd;
padding: 10px;
cursor: pointer;
}
.element2 {
background-color: #aaa;
transition: height 0.5s, width 0.5s;
height: 0;
width: 0;
}
function showElement() {
var element2 = document.querySelector('.element2');
element2.style.height = '200px';
element2.style.width = '200px';
}
该方法通过将第二个元素的height和width属性设置为0,使其默认隐藏且不占用空间。然后,在第一个元素的点击事件中,将第二个元素的height和width属性设置为需要的值,并通过transition属性实现过渡效果,从而实现点击显示的效果。
原文地址: https://www.cveoy.top/t/topic/napm 著作权归作者所有。请勿转载和采集!