Box 1
Box 2
.box1 {
  width: 100px;
  height: 100px;
  background-color: red;
  cursor: pointer;
  transition: opacity 0.5s;
}

.box2 {
  width: 100px;
  height: 100px;
  background-color: blue;
  opacity: 0;
  transition: opacity 0.5s;
}
function showBox2() {
  var box2 = document.querySelector('.box2');
  box2.style.opacity = '1';
}

在初始状态下,box2 的 opacity 为 0,不显示也不占据空间。当点击 box1 时,通过 JavaScript 设置 box2 的 opacity 为 1,使其逐渐显示并占据空间。过渡效果使用 opacity 属性的过渡。

HTML 同级元素隐藏显示,无 JavaScript 复杂操作

原文地址: https://www.cveoy.top/t/topic/napl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录