CSS3實現的動態內容標簽頁切換效果


內容標簽頁在網站或者web開發中經常使用到,它對於幫助我們美化頁面非常的實用。這個教程中我們將使用radio button和:checked偽類和sibling組合來實現一個CSS3內容標簽頁。

 

注意目前並不是所有的瀏覽器都支持CSS3。


HTML標簽 

這里我們使用input元素來生成內容切換操作元素,並且使用label元素來生成標簽頁內容:

 

<section class="tabs">
    <input id="tab-1" type="radio" name="radio-set" class="tab-selector-1" checked="checked" />
    <label for="tab-1" class="tab-label-1">About us</label>

    <input id="tab-2" type="radio" name="radio-set" class="tab-selector-2" />
    <label for="tab-2" class="tab-label-2">How we work</label>

    <input id="tab-3" type="radio" name="radio-set" class="tab-selector-3" />
    <label for="tab-3" class="tab-label-3">References</label>

    <input id="tab-4" type="radio" name="radio-set" class="tab-selector-4" />
    <label for="tab-4" class="tab-label-4">Contact us</label>

    <div class="clear-shadow"></div>

    <div class="content">
        <div class="content-1">
            <p>Some content</p>
        </div>
        <div class="content-2">
            <p>Some content</p>
        </div>
        <div class="content-3">
            <p>Some content</p>
        </div>
        <div class="content-4">
            <p>Some content</p>
        </div>
    </div>
</section> 

 

每一個input元素都包含一個數值,我們可以通過checked屬性添加缺省的值。


CSS樣式 

首先我們需要定義尺寸並且通過設置opacity:0來隱藏input

 

tabs {
    position: relative;
    margin: 40px auto;
    width: 750px;
}

.tabs input {
    position: absolute;
    z-index: 1000;
    width: 120px;
    height: 40px;
    left: 0px;
    top: 0px;
    opacity: 0;
    cursor: pointer;
}
.tabs input#tab-2{
    left: 120px;
}
.tabs input#tab-3{
    left: 240px;
}
.tabs input#tab-4{
    left: 360px;
} 

  

input元素將會覆蓋label。這樣當我們點擊label的時候,其實是點擊input。這個小技巧同樣可以使用在mobile瀏覽器中(有些瀏覽器,點擊label將不會對input產生效果)。

 

接下來,我們將通過定義一些樣式使label看起來像標簽頁。注意每一個label都有一個不同的z-index。一個box-shadow將會添加深度和真實感到標簽頁。

 

.tabs label {
    background: linear-gradient(top, #5ba4a4 0%,#4e8c8a 100%);
    font-size: 15px;
    line-height: 40px;
    height: 40px;
    position: relative;
    padding: 0 20px;
    float: left;
    display: block;
    width: 80px;
    color: #385c5b;
    letter-spacing: 1px;
    text-transform: uppercase;
    font-weight: bold;
    text-align: center;
    text-shadow: 1px 1px 1px rgba(255,255,255,0.3);
    border-radius: 3px 3px 0 0;
    box-shadow: 2px 0 2px rgba(0,0,0,0.1), -2px 0 2px rgba(0,0,0,0.1);
}

.tabs input:hover + label {
    background: #5ba4a4;
}

.tabs label:first-of-type {
    z-index: 4;
    box-shadow: 2px 0 2px rgba(0,0,0,0.1);
}

.tab-label-2 {
    z-index: 3;
}

.tab-label-3 {
    z-index: 2;
}

.tab-label-4 {
    z-index: 1;
}

 

因為我們不想顯示box-shadow的底部,這里我們將使用一個:after偽類來覆蓋:
.tabs label:after {
    content: '';
    background: #fff;
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 100%;
    height: 2px;
    display: block;
} 

  

當我們點擊一個標簽頁時,將會有不同的樣式和顏色。最重要的一點在於確認"checked" label將會被置於所有標簽頁的頂層。因此,這里我們修改z-index:

 

.tabs input:checked + label {
    background: #fff;
    z-index: 6;
} 

 

上面我們提到過,內容部分將包含了所有的標簽頁面,我們將設置z-index到5,正好處於選擇的label的下方。使用這種方式,box-shadow內容將會覆蓋其它的label。

 

在內容區域,這里有四個部分,每一個都有自己的內容。缺省,我們隱藏他們。因此,我們設置opacity為0並且z-index為1。我們不能使用display:none,因為這個屬性不支持過渡效果。
.content {
    background: #fff;
    position: relative;
    width: 100%;
    height: 370px;
    z-index: 5;
    box-shadow: 0 -2px 3px -2px rgba(0,0,0,0.2), 0 2px 2px rgba(0,0,0,0.1);
    border-radius: 0 3px 3px 3px;
}

.content div {
    position: absolute;
    top: 0;
    left: 0;
    padding: 10px 40px;
    z-index: 1;
    opacity: 0;
    transition: all linear 0.1s;
}

.content div h2,
.content div h3{
    color: #398080;
}
.content div p {
    font-size: 14px;
    line-height: 22px;
    font-style: italic;
    text-align: left;
    margin: 0;
    color: #777;
    padding-left: 15px;
    font-family: Cambria, Georgia, serif;
    border-left: 8px solid rgba(63,148,148, 0.1);
}
 
當我們想要一個內容出現,我們設置opacity為1,並且將z-index值變大。

 

.tabs input.tab-selector-1:checked ~ .content .content-1,
.tabs input.tab-selector-2:checked ~ .content .content-2,
.tabs input.tab-selector-3:checked ~ .content .content-3,
.tabs input.tab-selector-4:checked ~ .content .content-4 {
    z-index: 100;
    opacity: 1;
    transition: all ease-out 0.2s 0.1s;
} 

 

 

在這個教程中,我們介紹了基本的淡入/淡出的例子


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM