前端——切换轮播图

学完前端js小知识后,动手操作的一个简单图片轮播图。

<!DOCTYPE html>
<html lang="zh-CN">

    <head>
        <meta charset="UTF-8">
        <meta name="keywords" content="关键词信息">
        <meta name="description" content="描述信息">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>轮播图切换</title>
        <style>
            * {
                margin: 0;
                padding: 0;
                list-style: none;
            }

            a {
                text-decoration: none;
                color: black;
            }

            .Rotation {
                position: relative;
                width: 900px;
                height: 400px;
                border: 1px solid red;
                margin: 100px auto;
            }

            .Rot_img {
                position: relative;
                width: 100%;
                height: 100%;
            }

            .Rot_img>li {
                opacity: 0;
                position: absolute;
                width: 100%;
                height: 100%;
            }

            .Rot_img>li>img {
                width: 100%;
                height: 100%;
            }

            .Rotation>div {
                z-index: 2;
                position: absolute;
                top: 50%;
                width: 40px;
                height: 40px;
                background-color: rgba(0, 0, 0, 0.5);
                line-height: 40px;
                font-size: 28px;
                text-align: center;
                color: #ccc;
                cursor: pointer;
            }

            .left_btn {
                left: 0px;
            }

            .right_btn {
                right: 0px;
            }

            .Rot_img>.show {
                z-index: 1;
                opacity: 1;
            }
        </style>
    </head>

    <body>
        <div class="Rotation">
            <ul class="Rot_img">
                <li class="show">
                    <img src="./img/1.png" alt="">
                </li>
                <li>
                    <img src="./img/2.png" alt="">
                </li>
                <li>
                    <img src="./img/3.png" alt="">
                </li>
            </ul>
            <div class="left_btn">&lt;</div>
            <div class="right_btn">&gt;</div>
        </div>
        <script>
            /* 
             原理:  把li全部定位在一起(设置透明度为0), 当你点击需要展示的li 给它添加上类名并且(透明值为1, 层级也为1)
           */
            //    获取左侧按钮
            let leftBtn = document.querySelector('.left_btn'),
            //   右侧按钮
             rightBtn =document.querySelector('.right_btn'),
            //  获取li标签  
            listBox = document.querySelectorAll('.Rot_img>li'),
            // 记录我单前点击的下标 
            index = 0;


            leftBtn.onclick = function(){
                //  console.log(1);
                // 让单前的li移出类名 show
                listBox[index].classList.remove('show');

                // // 0的前面往左边 是 -1   那这个数组是没有负1值的  
                // index--;

                // // // 进行判断  
                // if(index == -1){
                //     // 如果这个index下标值为负1  强制让他等于这个 数组值-1 
                //     index =listBox.length-1;
                // }

                // 先运算后判断   那先判断再运算  
                if(index == 0){
                    // 如果是等于0  那就让他等于当前数组长度  
                    index = listBox.length;
                }
                index--;
                // 然后在让下一个li加上类名  
                listBox[index].classList.add('show')
            }


            // 
            rightBtn.onclick = function(){
                // 让单前的li移出类名 show
                listBox[index].classList.remove('show');
                // 下标加1   向左侧走 单前为下标为0  + 1 
                index++;
            
                // 判断 如果大于当前数组
                if(index>listBox.length-1){
                    // 强制等于0
                    index = 0;
                }
       
                // 然后在让下一个li加上类名  
                listBox[index].classList.add('show')
            }



        </script>
    </body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值