选项卡切换效果,点击切换图片

今天分享一篇前端开发实战当中经常使用到的选项卡切换效果的文章,希望对您有所帮助,欢迎留言探讨。

1、html结构布局:
<div id="box">
    <div class="img">
        <ul>
            <li class="active"><img src="img/1.jpg" /></li>
            <li><img src="img/2.jpg" /></li>
            <li><img src="img/3.jpg" /></li>
            <li><img src="img/4.jpg" /></li>
        </ul>
        <p><span id="index">1</span>/4</p>
    </div>
    <div class="btn">
        <ul>
            <li class="active">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
    </div>
</div>


2、css布局样式:
<style>
  *{ margin:0; padding:0; font-family:"Microsoft yahei",serif;}
    li{ list-style-type: none;}
    #box{
        position: relative;
        width: 350px;
        height: 580px;
        margin: 50px auto;
    }
    #box .img{
        position: absolute;
        top: 0;
        left: 0;
        width: 326px;
        height: 580px;
    }
    #box .img li{
        display: none;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }
    #box .img li.active{
        display: block;
    }
    #box .img li img{
        display: block;
    }
    #box .img p{
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 35px;
        text-align: center;
        background: rgba(0,0,0,.5);
        color: #fff;
        font-size: 14px;
        font-weight: bold;
        line-height: 35px;
    }
    #box .btn{
        position: absolute;
        top: 0;
        right: 0;
    }
    #box .btn ul li{
        width: 24px;
        height: 24px;
        margin-bottom: 10px;
        line-height: 24px;
        color: #fff;
        font-weight: bold;
        font-size: 12px;
        text-align: center;
        background: #000;
        cursor: pointer;
    }
    #box .btn ul li.active{
        background: #f60;
    }
</style>
3、js行为实现:
<script>

    var aBtn = document.getElementsByClassName("btn")[0].getElementsByTagName("li"),
        aImg = document.getElementsByClassName("img")[0].getElementsByTagName("li"),
        oTxt = document.getElementById("index"),
        length = aBtn.length,
        index = 0;//这个变量用来存储当前显示的序号

    for (var i = 0; i < length; i++)
    {
        aBtn[i].goudan = i;//自定义属性,用来存每个人的序号
        aBtn[i].onclick = function () {
            if ( this.goudan !== index ){
                aBtn[index].className = "";//把前一个的名字去掉
                aImg[index].className = "";//把前一图片的名字去掉

                index = this.goudan;//把序号变成当前点击的这个的序号

                aBtn[index].className = "active";//给点击的这个加名字
                aImg[index].className = "active";//给当前要显示的图片加名字

                oTxt.innerHTML = index+1;
            }
        };
    }

</script>
3、实现效果:

在这里插入图片描述

:: 更多内容 ::

> 开发过程中踩坑经验记录

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值