使用max-height隐藏内容以隐藏下拉列表

时间:2018-05-22 20:26:10

标签: javascript jquery css

我正在尝试创建一个可以上下切换的下拉列表。

我现在的问题是内容列表总是显示,我试图让它只显示它的上升和上升。正如你所看到的,我已经将最大高度设置为0,然后在切换时将其设置为65vh,但是最大高度0似乎没有成功。

我也尝试将max-height更改为仅使用高度,但是该动画不起作用,但动画不适用于高度值。

const chooseAreaButton = document.querySelector('.ChooseArea');

$('.ChooseArea .wrapper .parent').click(function() {

  $(chooseAreaButton).toggleClass('top', 400);
  $('.ChooseArea .wrapper .content').toggleClass('active');

});
.Container {
  height: 100vh;
  width: 100vw;
  position: absolute;
  justify-content: center;
  align-items: center;
  pointer-events: none;
  display: flex;
}

.Modal {
  padding: 5%;
  box-sizing: border-box;
  height: 70vh;
  width: 87vw;
  border-radius: 15px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: column;
  align-items: center;
  background: white;
  overflow: hidden;
  // transform: translateX(-900px);
  transform: translateX(0);
  transition: all 0.2s ease-in-out;
}

.ChooseArea.top {
  top: 0;
}

.ChooseArea {
  position: absolute;
  height: 7%;
  margin-bottom: 5%;
  pointer-events: visible;
  width: 70%;
  display: flex;
  justify-content: center;
  bottom: 0;
}

.wrapper {
  display: inline-block;
  width: 100%;
  height: 100%;
  position: relative;
}

.parent {
  height: 100%;
  width: 100%;
  display: block;
  cursor: pointer;
  line-height: 30px;
  height: 30px;
  border-bottom: 1px solid $primary;
  color: #000;
  z-index: 999;
  position: relative;
  -webkit-transition: border-radius 0.1s linear, background 0.1s linear, z-index 0s linear;
  -webkit-transition-delay: 0s;
  text-align: left;
  font-size: 18px;
}

.content {
  position: absolute;
  top: 0;
  display: block;
  z-index: 1;
  max-height: 0;
  width: 100%;
  padding-top: 30px;
  transition: max-height 0.4s linear;
}

.content.active {
  z-index: 3;
  max-height: 65vh;
  overflow: scroll;
}

.content ul {
  background: green;
  margin: 0;
  padding: 0;
  overflow: hidden;
  height: 100%;
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.3);
}

.content ul a {
  text-decoration: none;
}

.content li:hover {
  background-color: rgba(223, 223, 223, 1) !important;
}

.content li {
  list-style: none;
  text-align: left;
  color: #888;
  font-size: 14px;
  height: 8vh;
  padding-left: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.content li:last-of-type {
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<div class="Container">
  <div class="Modal">
    <div class="ChooseArea bottom">
      <div class="wrapper">
        <div class="content">
          <ul>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
          </ul>
        </div>
        <div class="parent">Choose test</div>
      </div>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

您需要添加的唯一内容是overflow: hidden;,请参阅代码段:

&#13;
&#13;
const chooseAreaButton = document.querySelector('.ChooseArea');

$('.ChooseArea .wrapper .parent').click(function() {
  $(chooseAreaButton).toggleClass('top', 400);
  $('.ChooseArea .wrapper .content').toggleClass('active');
});
&#13;
.Container {
  height: 100vh;
  width: 100vw;
  position: absolute;
  justify-content: center;
  align-items: center;
  pointer-events: none;
  display: flex;
}

.Modal {
  padding: 5%;
  box-sizing: border-box;
  height: 70vh;
  width: 87vw;
  border-radius: 15px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: column;
  align-items: center;
  background: white;
  overflow: hidden;
  // transform: translateX(-900px);
  transform: translateX(0);
  transition: all 0.2s ease-in-out;
}

.ChooseArea.top {
  top: 0;
}

.ChooseArea {
  position: absolute;
  height: 7%;
  margin-bottom: 5%;
  pointer-events: visible;
  width: 70%;
  display: flex;
  justify-content: center;
  bottom: 0;
}

.wrapper {
  display: inline-block;
  width: 100%;
  height: 100%;
  position: relative;
}

.parent {
  height: 100%;
  width: 100%;
  display: block;
  cursor: pointer;
  line-height: 30px;
  height: 30px;
  border-bottom: 1px solid $primary;
  color: #000;
  z-index: 999;
  position: relative;
  -webkit-transition: border-radius 0.1s linear, background 0.1s linear, z-index 0s linear;
  -webkit-transition-delay: 0s;
  text-align: left;
  font-size: 18px;
}

.content {
  position: absolute;
  top: 0;
  display: block;
  z-index: 1;
  max-height: 0;
  overflow: hidden; /* <-- ADDED HERE */
  width: 100%;
  padding-top: 30px;
  transition: max-height 0.4s linear;
}

.content.active {
  z-index: 3;
  max-height: 65vh;
  overflow: scroll;
}

.content ul {
  background: green;
  margin: 0;
  padding: 0;
  overflow: hidden;
  height: 100%;
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.3);
}

.content ul a {
  text-decoration: none;
}

.content li:hover {
  background-color: rgba(223, 223, 223, 1) !important;
}

.content li {
  list-style: none;
  text-align: left;
  color: #888;
  font-size: 14px;
  height: 8vh;
  padding-left: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.content li:last-of-type {
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
}
&#13;
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<div class="Container">
  <div class="Modal">
    <div class="ChooseArea bottom">
      <div class="wrapper">
        <div class="content">
          <ul>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
            <a href="#"><li>Test</li></a>
          </ul>
        </div>
        <div class="parent">Choose test</div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

希望它有所帮助!

答案 1 :(得分:0)

如果我正确理解您的问题,您可以将bottom改为ChooseArea改为10px。代码如下。

您根本不需要使用active课程来实现这一目标。

&#13;
&#13;
const chooseAreaButton = document.querySelector('.ChooseArea');

$('.ChooseArea .wrapper .parent').click(function(){

  $(chooseAreaButton).toggleClass('top',400);

});
&#13;
.Container {
  height: 100vh;
  width: 100vw;
  position: absolute;
  justify-content: center;
  align-items: center;
  pointer-events: none;
  display: flex;
}
.Modal {
  padding: 5%;
  box-sizing: border-box;
  height: 70vh;
  width: 87vw;
  border-radius: 15px;
  box-shadow: 0 2px 4px 0 rgba(0,0,0,0.3);
  display: flex;
  flex-direction: column;
  align-items: center;
  background: white;
  overflow: hidden;
  // transform: translateX(-900px);
  transform: translateX(0);
  transition: all 0.2s ease-in-out;
  }
  .ChooseArea.top {
    top: 0px;
  }
.ChooseArea {
    position: absolute;
    height: 7%;
    margin-bottom: 5%;
    pointer-events: visible;
    width: 70%;
    display: flex;
    justify-content: center;
    bottom: -10px;
    }
    .wrapper {
      display: inline-block;
      width: 100%;
      height: 100%;
      position: relative;
    }
        .parent {
      height: 100%;
      width: 100%;
      display: block;
      cursor: pointer;
      line-height: 30px;
      height: 30px;
      border-bottom: 1px solid $primary;
      color: #000;
      z-index: 999;
      position: relative;
      -webkit-transition: border-radius 0.1s linear, background 0.1s linear, z-index 0s linear;
      -webkit-transition-delay: 0s;
      text-align: left;
      font-size: 18px;
      }
      .content {
      position: absolute;
      top: 0;
      display: block;
      z-index: 1;
      max-height: 0;
      width: 100%;
      padding-top: 30px;
      transition: max-height 0.4s linear;
    }
    
    .content ul {
      background: green;
      margin: 0;
      padding: 0;
      overflow: hidden;
      height: 100%;
      border-bottom-left-radius: 5px;
      border-bottom-right-radius: 5px;
      box-shadow: 0 2px 4px 0 rgba(0,0,0,0.3);
    }
        .content ul a {
      text-decoration: none;
    }

    .content li:hover {
      background-color: rgba(223,223,223,1) !important;
    }
        .content li {
      list-style: none;
      text-align: left;
      color: #888;
      font-size: 14px;
      height: 8vh;
      padding-left: 10px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      }
      .content li:last-of-type {
      border-bottom-left-radius: 5px;
      border-bottom-right-radius: 5px;
    }
&#13;
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
  <div class="Container">
    <div class="Modal">
<div class="ChooseArea bottom">
        <div class="wrapper">
          <div class="content">
              <ul>
                <a href="#"><li>Test</li></a>
                <a href="#"><li>Test</li></a>
                <a href="#"><li>Test</li></a>
                <a href="#"><li>Test</li></a>
                <a href="#"><li>Test</li></a>
                <a href="#"><li>Test</li></a>
                <a href="#"><li>Test</li></a>
                <a href="#"><li>Test</li></a>
                <a href="#"><li>Test</li></a>
  
              </ul>
          </div>
              <div class="parent">Choose test</div>
        </div>
      </div>
      </div>
      </div>
&#13;
&#13;
&#13;