javascript - 使用 js 和 css 隐藏/显示 div 中的内容

标签 javascript html css

我想通过单击图像的另一个 div 来显示一个 div 中的内容。我有基本的想法,但是当我将内容放入 div 时,当 div 关闭时它不会隐藏。

我放了一个<ul>在和<ul>即使在隐藏 div 时也始终显示。

$(document).ready(function () {
  $('#header').click(function () {
    $('#content').removeClass('hidden');
    $('#content').addClass('visible');
  });
  $('#content').on('mouseleave', function (e) {
    setTimeout(function () {
      $('#content').removeClass('visible').addClass('hidden');
    }, 600);
  });
});
.hidden{
    max-height: 0px;
}
.visible{
    max-height: 500px;  
}

#content{
    width:200px;
    height:200px;
    background:teal;
    -webkit-transition: max-height 0.8s;
    -moz-transition: max-height 0.8s;
    transition: max-height 0.8s;
}

#header{
    width:10px;
    height:10px;
    background:darkred;
    margin:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header"></div>
<div id="content">
  <ul>
    <li>some text</li>
    <li>some text</li>
  </ul>
</div>

最佳答案

那是因为你的<ul>溢出<div>高度。添加overflow: hidden#content

$(document).ready(function () {
  $('#header').click(function () {
    $('#content').removeClass('hidden');
    $('#content').addClass('visible');
  });
  $('#content').on('mouseleave', function (e) {
    setTimeout(function () {
      $('#content').removeClass('visible').addClass('hidden');
    }, 600);
  });
});
.hidden{
    max-height: 0px;
}
.visible{
    max-height: 500px;  
}

#content{
    width:200px;
    height:200px;
    background:teal;
    -webkit-transition: max-height 0.8s;
    -moz-transition: max-height 0.8s;
    transition: max-height 0.8s;
    overflow: hidden;
}

#header{
    width:10px;
    height:10px;
    background:darkred;
    margin:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header"></div>
<div id="content">
  <ul>
    <li>some text</li>
    <li>some text</li>
  </ul>
</div>

关于javascript - 使用 js 和 css 隐藏/显示 div 中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39162232/

相关文章:

javascript - 使用 Jade Html 预处理器分别在每个 div 内包含一个 img 标签的多个 Div

css - 如何在 :before pseudo element of an element? 上添加 svg 作为内容

javascript - 是否有必要在每个 `toLowerCase()` 之后调用 `typeof` ?

javascript - 在响应式网格站点中拥有全屏宽度背景

javascript - 返回数字中第 n 位数字的最佳方法?

jquery - 删除多个类到 "Reset"Div

html - Safari 占用提交按钮周围的空间

永远不会调用 JavaScriptInterface 类在 webview android 上加载 html

javascript - 在 textarea 中使用类似 span 的样式

javascript - 根据点击的 anchor /元素分配 php 变量值