【问题标题】:Collapsible Panel in HTML/CSS [closed]HTML / CSS中的可折叠面板[关闭]
【发布时间】:2013-05-06 05:43:54
【问题描述】:

我正在建立一个网站。我需要帮助创建以下功能:

我希望“关于”链接在单击时展开到面板中,并在用户按面板中的“隐藏”时收回。我在下面附上了一张图表,以阐明它应该是什么样子。当用户在(1)中按下About时,它变成了(2),当用户在(2)中按下hide时,它又变成了(1)。

如果可能的话,我想用纯 HTML/CSS 来做这件事。有谁知道我该怎么做?

【问题讨论】:

  • @Mr.Alien:这非常无用。这是一个非常基本的功能。实施起来并不难。
  • 我们不是从头开始写东西的,所以你需要想出你的代码不起作用,我们会为你修复它,所以你自己试试,如果你卡住了,问这里有一个问题,会帮助你
  • 因为我不知道该怎么做。这就是我寻求帮助的原因。我对 HTML 和 CSS 不是很有经验。
  • 或者阅读一些书籍、在线教程、文档。在您在这里提出问题之前,您至少应该想出一些东西(代码)。
  • 这个问题已经得到了很好的回答。

标签: html css


【解决方案1】:

你需要很少的 javascript 来触发事件(显示/隐藏 div)

<a href="#"> Home </a>

<a class="right" href="javascript:toggle_messege('inline')" id='href_about'> About </a>
<br />
<a class="right hide" href="javascript:toggle_messege('none')" id='hreh_close'> (Close)</a>

<div id='div_messege' class='hide'>Hidden messege to show, Hidden messege to show Hidden messege to show Hidden messege to show</div>
<p>Test Test TestTestTestTestTestTestTest</p>
<p>Test Test TestTestTestTestTestTestTest</p>
<p>Test Test TestTestTestTestTestTestTest</p>
<p>Test Test TestTestTestTestTestTestTest</p>
<p>Test Test TestTestTestTestTestTestTest</p>

CSS

.right {
    float:right;
}
.hide {
    display:none
}

javascript

function toggle_messege(type) {
 document.getElementById("div_messege").style.display = type;
    document.getElementById("hreh_close").style.display = type;

}

检查此运行示例http://codepen.io/faishal/pen/IHEyw

【讨论】:

  • 谢谢!这也很有帮助。
  • 我无法让 CSS 跨浏览器可靠地工作。感谢您提供有关使用 javascript 的提示,这很好用!
  • 您不需要 Javascript,正如 adaam 和 Thurstan's 的回答所证明的那样。
【解决方案2】:

这个答案解释了如何完全实现它:Pure CSS collapse/expand div

这里是一个简短的纲要:

<div class="FAQ">
    <a href="#hide1" class="hide" id="hide1">+</a>
    <a href="#show1" class="show" id="show1">-</a>
    <div class="question"> Question Question Question Question Question Question Question Question Question Question Question? </div>
        <div class="list">
            <p>Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer </p>
        </div>
</div>

CSS

/* source: http://www.ehow.com/how_12214447_make-collapsing-lists-java.html */

.FAQ { 
  vertical-align: top; 
  height: auto; 
}

.list {
  display:none; 
  height:auto;
  margin:0;
  float: left;
}

.show {
  display: none; 
}

.hide:target + .show {
  display: inline; 
}
.hide:target {
  display: none; 
}
.hide:target ~ .list {
  display:inline; 
}

/*style the (+) and (-) */
.hide, .show {
  width: 30px;
  height: 30px;
  border-radius: 30px;
  font-size: 20px;
  color: #fff;
  text-shadow: 0 1px 0 #666;
  text-align: center;
  text-decoration: none;
  box-shadow: 1px 1px 2px #000;
  background: #cccbbb;
  opacity: .95;
  margin-right: 0;
  float: left;
  margin-bottom: 25px;
}

.hide:hover, .show:hover {
  color: #eee;
  text-shadow: 0 0 1px #666;
  text-decoration: none;
  box-shadow: 0 0 4px #222 inset;
  opacity: 1;
  margin-bottom: 25px;
}

.list p {
  height:auto;
  margin:0;
}
.question {
  float: left;
  height: auto;
  width: 90%;
  line-height: 20px;
  padding-left: 20px;
  margin-bottom: 25px;
  font-style: italic;
}

还有工作的 jsFiddle:

http://jsfiddle.net/dmarvs/94ukA/4/

以上所有内容都不是我的工作,只是为了澄清,但它只是表明在 Google 上找到它是多么容易!!

【讨论】:

  • 谷歌把我带到了这里。只是说。
  • 如果你只有一行,这很好用。但是,如果您有多行(每行上都有一个 + 按钮),您怎么能做到这一点呢?
  • @ChrisNielsen 我只会使用 JavaScript(这个答案是很久以前的了!!)。但这里是多个 CSS 唯一版本:jsfiddle.net/94ukA/4535 :target 选择器从 href 捕获 ID
猜你喜欢
  • 2012-06-03
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多