了解如何創建一個手風琴(collapsible content) 。
手風琴
當你想隱藏和顯示大量的內容之間進行切換手風琴是有用的:
Lorem存有悲坐阿梅德,consectetur adipisicing ELIT,sed的根本eiusmod tempor incididunt UT labore等dolore麥格納aliqua。 UT enim廣告微量veniam,QUIS nostrud實習ullamco laboris暫準UT aliquip前EA commodo consequat。
Lorem存有悲坐阿梅德,consectetur adipisicing ELIT,sed的根本eiusmod tempor incididunt UT labore等dolore麥格納aliqua。 UT enim廣告微量veniam,QUIS nostrud實習ullamco laboris暫準UT aliquip前EA commodo consequat。
Lorem存有悲坐阿梅德,consectetur adipisicing ELIT,sed的根本eiusmod tempor incididunt UT labore等dolore麥格納aliqua。 UT enim廣告微量veniam,QUIS nostrud實習ullamco laboris暫準UT aliquip前EA commodo consequat。
創建一個手風琴
步驟1)添加HTML:
例
<button class="accordion">Section 1</button>
<div class="panel">
<p>Lorem
ipsum...</p>
</div>
<button class="accordion">Section
2</button>
<div class="panel">
<p>Lorem ipsum...</p>
</div>
<button class="accordion">Section 3</button>
<div class="panel">
<p>Lorem
ipsum...</p>
</div>
步驟2)添加CSS:
風格手風琴:
例
/* Style the buttons that are used to open and close the accordion panel */
button.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;
}
/* Add a background color to the button if it is clicked on (add the
.active class with JS), and when you move the mouse over it (hover) */
button.accordion.active, button.accordion:hover {
background-color: #ddd;
}
/* Style the accordion panel. Note:
hidden by default */
div.panel {
padding: 0 18px;
background-color: white;
display: none;
}
/* The "show" class is added to the
accordion panel when the user clicks on one of the buttons. This
will show the panel content */
div.panel.show {
display: block !important;
}
步驟3)添加JavaScript的:
例
/* Toggle between adding and removing the
"active" and "show" classes when the user clicks on one of the
"Section"
buttons. The "active" class is used to add a background color to the
current button when its belonging panel is open. The "show" class is used to open the
specific accordion panel */
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick =
function(){
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
試一試» 動畫手風琴
進行以下更改panel
和show
類,使動畫手風琴。
這將使手風琴滑下(max-height)和淡入(opacity) :
例
div.panel {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition:
0.6s ease-in-out;
opacity: 0;
}
div.panel.show {
opacity: 1;
max-height:
500px; /* Whatever you like, as long as its more than the height of the
content (on all screen sizes) */
}
試一試» 添加圖標
一個符號添加到每個按鈕指示可折疊的內容是打開還是閉合的:
例
button.accordion:after {
content: '\02795'; /* Unicode
character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;
}
button.accordion.active:after {
content: "\2796"; /*
Unicode character for "minus" sign (-) */
}
試一試»