使用CSS3中的input标签与lable标签组合实现banner图的切换

  在作网页时,咱们常常能够碰到banner图的切换。对于那些JS薄弱的同窗来讲,这就很尴尬了。今天,我来告诉你们如何使用CSS作出banner图切换的效果。app

  这里,用到了lable标签与input的组合,首先先来了解下lable标签布局

    <label> 标签为 input 元素定义标签(label)。spa

    label 元素不会向用户呈现任何特殊的样式。不过,它为鼠标用户改善了可用性,由于若是用户点击 label 元素内的文本,则会切换到控件自己。code

    <label> 标签的 for 属性应该等于相关元素的 id 元素,以便将它们捆绑起来。blog

  input标签有个checked属性,当lable属性绑定后,选中lable便可选中input标签图片

  首先,先作一个基本的HTML布局ci

 

<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的绑定input

 

加以修饰it

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使图片上去.io

对lable稍加修饰,就能够拿去网页上用了,不用复杂的JS代码啦(ps:对lable修饰时,须要设置display:block属性)。

相关文章
相关标签/搜索