2013-05-11 117 views
17

我正在组建一个网站。我需要帮助创建以下功能:HTML/CSS中的可折叠面板

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

layout

我想如果可能这样做纯HTML/CSS。有谁知道我该怎么做?

+0

@ Mr.Alien:这非常无益。这是一个非常基本的功能。实施起来并不困难。 – xisk 2013-05-11 19:29:45

+0

那么为什么不尝试自己呢?我们不会从头开始编写任何东西,所以您需要提供您的代码,这些代码不起作用,我们会为您解决它,所以请自己尝试,如果您遇到困难,请在此提出问题并帮助您 – 2013-05-11 19:30:26

+0

因为我不知道该怎么做。这就是我寻求帮助的原因。我对HTML和CSS不是很有经验。 – xisk 2013-05-11 19:30:55

回答

22

这样的回答解释了如何可以完全实现: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 !important; 
} 
.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/

再次没有一个以上是我的工作只是澄清,但它只是表明它是多么容易找到它在谷歌!

+10

Google把我带到了这里。只是说。 – 2015-01-12 09:38:34

3

您需要痘痘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

+1

谢谢!这也有帮助。 – xisk 2013-05-11 20:34:51

+0

我无法让CSS在各种浏览器中可靠地工作。感谢关于使用JavaScript的提示,这个效果很好! – Mmm 2015-11-11 21:18:56

+0

你不需要Javascript,就像adaam和[Thurstan's](https://stackoverflow.com/a/15742338/2230956)答案所示。 – Gid 2018-01-10 04:50:30