使用js实现轮播图

  • CSS代码:
*{
            margin: 0;
            padding: 0;
        }
        #picBox{
            position: relative;
            margin: 30px;
            width: 184px;
            height: 147px;
        }
        #picBox a{
            position: absolute;
            left: 0;
            top: 0;
            display: none;
        }
        #picBox .show{
            display: block;
        }
        .choice{
            position: absolute;
            bottom: 5px;
            left: 50%;
            margin-left: -26px;
        }
        .choice li{
            list-style: none;
            width: 15px;
            height: 15px;
            border-radius: 50%;
            display: inline-block;
            background: #fff;
            cursor: pointer;
        }
        .active{
            /* 【!important】  给一个样式加一个最高级 */
            background: red !important;
        }
        .left{
            width: 20px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            background: #999;
            color: #fff;
            opacity: 0.9;
            position: absolute;
            left: 0;
            top: 50%;
            margin-top: -15px;
            cursor: pointer;
        }
        .right{
            width: 20px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            background: #999;
            color: #fff;
            opacity: 0.9;
            position: absolute;
            right: 0;
            top: 50%;
            margin-top: -15px;
            cursor: pointer;
        }
  • body代码:
<div id="picBox">
        <a href="" class="show">
            <img src="./img/1.png" alt="">
        </a>
        <a href="">
            <img src="./img/2.png" alt="">
        </a>
        <a href="">
            <img src="./img/3.png" alt="">
        </a>
        <ul class="choice">
            <li class="active"></li>
            <li></li>
            <li></li>
        </ul>
        <div class="left"><</div>
        <div class="right">></div>
    </div>
  • js代码:
<script>
        // 当前显示的图片的索引为0
        var ind=0;
        $(".choice li").click(function(){
            // 选中当前选中的那个li的索引
            var index=$(this).index();
            // 【同步】(index 和 ind)两个索引
            ind=index;
            // // $(".choice li").eq(index)  等同于 $(this)
            // // siblings() 子节点
            // $(this).addClass("active").siblings("li").removeClass("active");
            // // 层级关系    eq() 当前选中的值 
            // $("#picBox a").eq(index).fadeIn(1000).siblings("a").fadeOut(1000);
            show(ind);
        });

        // 全局变量 才能为空!!!
        var timer=null;
        function showImg(){
            // 定时器
            timer = setInterval(function(){
                ind++;
                // 如果ind大于a标签的额最大值,强制性从0开始执行
                if(ind > $("#picBox a").length-1 ){
                    // 本来(index 和 ind)两个索引不同步
                    ind=0;
                }
                // // siblings() 子节点   eq(ind) 当前的某个位置
                // $(".choice li").eq(ind).addClass("active").siblings("li").removeClass("active");
                // // 层级关系
                // $("#picBox a").eq(ind).fadeIn(1000).siblings("a").fadeOut(1000);
                show(ind);
            }, 5000);
        }
        // 注意下面的函数的执行顺序!!!
        showImg();

        // 鼠标移入事件
        $("#pocBox").mouseover(function(){
            // 清空定时器
            clearInterval(timer);
        });
        // 鼠标移出事件
        $("#pocBox").mouseout(function(){
            // 执行方法
            showImg();
        });

        // 上一张图片
        $(".left").click(function(){
            ind--;
            if(ind < 0 ){
                // 本来(index 和 ind)两个索引不同步
                ind=$(".choice li").length-1;
            }
            // // siblings() 子节点   eq(ind) 当前的某个位置
            // $(".choice li").eq(ind).addClass("active").siblings("li").removeClass("active");
            // // 层级关系
            // $("#picBox a").eq(ind).fadeIn(1000).siblings("a").fadeOut(1000);
            show(ind);
        });
        // 下一张
        $(".right").click(function(){
            ind++;
            if(ind > $(".choice li").length-1 ){
                // 本来(index 和 ind)两个索引不同步
                ind=0;
            }
            // // siblings() 子节点   eq(ind) 当前的某个位置
            // $(".choice li").eq(ind).addClass("active").siblings("li").removeClass("active");
            // // 层级关系
            // $("#picBox a").eq(ind).fadeIn(1000).siblings("a").fadeOut(1000);
            show(ind);
        });

        // 优化,抽取共同的代码
        function show(ind){
            // siblings() 子节点   eq(ind) 当前的某个位置
            $(".choice li").eq(ind).addClass("active").siblings("li").removeClass("active");
            // 层级关系
            $("#picBox a").eq(ind).fadeIn(1000).siblings("a").fadeOut(1000);
        }
    </script>
  • 效果图
    在这里插入图片描述
    【注意:引用js文件】
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值