2013-05-11 120 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