javascript - 如何部分显示 Div 并在单击时显示完整

标签 javascript html css

enter image description here

我想要一个 css 选项 目前我正在使用这个 css 类

.a{
   top:-102px;
   height: 140px !important;
}

.b{
   top:0px;
   height: 240px !important;
}

但这是从上到下打开 div,我希望它从下到上打开

任何人都可以帮助它非常重要 提前致谢

最佳答案

  1. 您需要将您的 div 包装在另一个 div 中
  2. 给父 div position:relative
  3. 给 child div position:absolute
  4. 使用 bottom:0 锚定子 div

演示 1 使用最少的 JavaScript:

  • classList.toggle()

演示 2 仅使用 CSS:

  • <label><input type='checkbox'> “触发”需要

演示 1(纯 JavaScript)

var x = document.querySelector('.x');

x.addEventListener('click', function(e) {
  this.classList.toggle('b');
});
.y {
  position: relative;
  height: 240px;
  width: 50px;
  background: brown;
}

.x {
  position: absolute;
  height: 140px;
  width: 50px;
  background: red;
  bottom: 0;
  transition: height .5s ease;
}

.b {
  height: 240px;
  transition: height .5s ease;
}
<div class='y'>
  <div class='x'>Click the Red</div>
</div>

演示2(纯CSS)

.y {
  position: relative;
  height: 240px;
  width: 50px;
  background: brown;
}

.x {
  position: absolute;
  height: 140px;
  width: 50px;
  background: red;
  bottom: 0;
  transition: height .5s ease;
}

#chx {
  display: none
}

#chx:checked+.y .x {
  height: 240px;
  transition: height .5s ease;
}
<input id='chx' type='checkbox'>
<div class='y'>
  <label for='chx'>
  <div class='x'>Click on the Red</div>
  </label>
</div>

关于javascript - 如何部分显示 Div 并在单击时显示完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45628757/

相关文章:

只有当 alert() 在带有 AJAX 的 Internet Explorer 中使用时,Javascript 才会更新 UI

php - json 解码不工作

javascript - 如何将图标附加到光标?

jquery - 漂亮的照片库在屏幕上打开得太低,并且当您向下滚动时会向下移动

javascript - 跳出 promise 链或走不同的路

javascript - JQuery 追加后隐藏

html - 为什么有背景的父元素不覆盖宽度大于视口(viewport)的子元素?

PHP 注册页面正在运行,但没有输入到数据库中

html - 在边框上放置边距 - css

javascript - 如何正确触发onClick事件