使用CSS3中的input標籤與lable標籤組合實現banner圖的切換

來源:互聯網
上載者:User

標籤:att   image   line   相關   css   put   需要   先來   point   

  在做網頁時,我們經常可以碰到banner圖的切換。對於那些JS薄弱的同學來說,這就很尷尬了。今天,我來告訴大家如何使用CSS做出banner圖切換的效果。

  這裡,用到了lable標籤與input的組合,首先先來瞭解下lable標籤

    <label> 標籤為 input 元素定義標籤(label)。

    label 元素不會向使用者呈現任何特殊的樣式。不過,它為滑鼠使用者改善了可用性,因為如果使用者點擊 label 元素內的文本,則會切換到控制項本身。

    <label> 標籤的 for 屬性應該等於相關元素的 id 元素,以便將它們捆綁起來。

  input標籤有個checked屬性,當lable屬性綁定後,選中lable即可選中input標籤

  首先,先做一個基本的HTML布局

 

<div class="main">            <input type="radio" name="p" id="name1" class="set_page1"/>            <input type="radio" name="p" id="name2" class="set_page2"/>            <input type="radio" name="p" id="name3" class="set_page3"/>            <input type="radio" name="p" id="name4" class="set_page4"/>            <input type="radio" name="p" id="name5" class="set_page5"/>                        <!--lable中的for需要綁定對應的input的id-->            <label for="name1" class="c1"></label>            <label for="name2" class="c2"></label>            <label for="name3" class="c3"></label>            <label for="name4" class="c4"></label>            <label for="name5" class="c5"></label>                        <div class="photo">                <ul>                    <li class="li1">                        <img src="img/green-tea-cream-frappuccino-20151022185851.jpeg"/>                    </li>                    <li>                        <img src="img/hot-caramel-macchiato-20151022185811.jpg"/>                    </li>                    <li>                        <img src="img/hot-green-tea-latte-20160819155511.jpg"/>                    </li>                    <li>                        <img src="img/Starbucks-Hazelnut-Latte-20150924183645.jpg"/>                    </li>                    <li>                        <img src="img/starbucks-flatwhite-20151026112356.jpg"/>                    </li>                </ul>            </div>        </div>

 

for綁定input的ID實現lable與input的綁定

 

加以修飾

label{    cursor: pointer;    display: inline-block;    opacity: 0.3;    height: 70px;    width: 70px;    margin-top: 100px;    background-color: red;}label:hover{opacity: 1;}input{display:none;}ul{    list-style: none;    padding: 0px;    height: 365px;    overflow: hidden;    margin-left: 480px;    position: relative;}

 

.set_page1:checked ~.photo ul li:nth-child(1){    position: absolute;    top: 0px;    z-index: 25;}.set_page2:checked ~.photo ul li:nth-child(2){    position: absolute;    top: 0;    z-index: 22;}.set_page3:checked ~.photo ul li:nth-child(3){    position: absolute;    top: 0;    z-index: 23;}.set_page4:checked ~.photo ul li:nth-child(4){    position: absolute;    top: 0;    z-index: 24;}.set_page5:checked ~.photo ul li:nth-child(5){    position: absolute;    top: 0;}

隱藏input,因為這裡我們只需用到input的checked屬性。選中lable時,與之綁定的Input也會處於checked狀態,這樣,我們只需利用checked屬性加上偽類別選取器。就可以實現banner圖的切換。由於定位的原因,會使後面的圖蓋住前面的圖,所以需要設定z-index使圖片上去.

對lable稍加修飾,就可以拿去網頁上用了,不用複雜的JS代碼啦(ps:對lable修飾時,需要設定display:block屬性)。

 

使用CSS3中的input標籤與lable標籤組合實現banner圖的切換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.