CSS3實(shí)現(xiàn)手風(fēng)琴效果锥累,我們需要用到一個(gè)新屬性 :target(目標(biāo)偽類選擇器)
E:target 選擇匹配E的所有元素竣贪,且匹配元素被相關(guān)URL指向
第一步 完成html布局
<div class="accordionMenu">
<div class="menuSection" id="brand">
<h2><a href="#brand">Brand</a></h2>
<p>Lorem ipsum dolor...</p>
</div>
<div class="menuSection" id="promotion">
<h2><a href="#promotion">Promotion</a></h2>
<p>Lorem ipsum dolor sit amet...</p>
</div>
<div class="menuSection" id="event">
<h2><a href="#event">Event</a></h2>
<p>Lorem ipsum dolor sit amet...</p>
</div>
</div>
效果如下 每個(gè)div內(nèi)有一個(gè)標(biāo)題和一個(gè)內(nèi)容,標(biāo)題內(nèi)包含了一個(gè)a,a的鏈接指向那個(gè)ID
第二步,加上一些基礎(chǔ)樣式
.accordionMenu{
background: #fff;
color: #424242;
font: 12px Arial, Verdana,sans-serif;
margin: 0 auto;
padding: 10px;
width: 500px;
}
.accordionMenu h2{
margin: 5px 0;
padding: 0;
position: relative;
}
制作一個(gè)向下的三角形(上一篇有寫如何繪制三角形)
.accordionMenu h2:before{
border: 5px solid #fff;
border-color: #fff transparent transparent;
content: "";
height: 0;
position: absolute;
right: 10px;
top: 15px;
width: 0;
}
給標(biāo)題內(nèi)容等添加一些樣式
.accordionMenu h2 a{
background: #8f8f8f;
background: -moz-linear-gradient(top,#cecece,#8f8f8f);
background: -webkit-gradient(linear,left,top,left,bottom.from(#cecece),to(#8f8f8f));
background: -webkit-linear-gradient(top,#cecece,#8f8f8f);
background: linear-gradient(top,#cecece,#8f8f8f);
color: #424242;
display: block;
font-size: 13px;
margin: 0;
padding: 10px 10px;
text-shadow:2px 2px 2px #aeaeae;
text-decoration: none;
}
.accordionMenu p{
margin: 0;
height: 0; /*設(shè)置高度為0,隱藏內(nèi)容*/
overflow: hidden;
padding: 0 10px;
-moz-transition:height 0.5s ease-in;
-webkit-transition:height 0.5s ease-in;
-o-transition:height 0.5s ease-in;
transition:height 0.5s ease-in;
}
這里transition 屬性就相當(dāng)于監(jiān)聽高度改變险掀,改變了height 運(yùn)動0.5s 以ease-in的方式
添加:target部分 實(shí)現(xiàn)點(diǎn)擊展開的效果
.accordionMenu :target h2 a,
.accordionMenu h2 a:focus,
.accordionMenu h2 a:hover,
.accordionMenu h2 a:active{
background: #2288dd;
background: -moz-linear-gradient(top,#6bb2ff,#2288dd);
background: -webkit-gradient(linear,left,top,left,bottom.from(#6bb2ff),to(#2288dd));
background: -webkit-linear-gradient(top,#6bb2ff,#2288dd);
background: linear-gradient(top,#6bb2ff,#2288dd);
color: #fff;
}
.accordionMenu :target p{
height: 100px;
overflow: auto;
}
.accordionMenu :target h2:before{
border-color: transparent transparent transparent;
}
最后效果
大家可以去試試