前端——切换轮播图

学完前端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>
相关推荐
wang_book1 小时前
uniapp学习(003-1 vue3学习 Part.1)
前端·学习·微信小程序·小程序·uni-app·vue
一一程序1 小时前
uniapp实现图片上下浮动效果
前端·uni-app·动画·animation
安冬的码畜日常1 小时前
【CSS in Depth 2 精译_041】6.4 CSS 中的堆叠上下文与 z-index(上)
前端·css·css3·html5·css定位·z-index·元素堆叠
花开不识君1 小时前
Chrome Cookie最大有效期
前端·chrome
小白求学11 小时前
CSS链接
前端·css
太阳火神的美丽人生2 小时前
WeChat_DevTools 断点调试方法总结
java·前端·微信
魏时烟3 小时前
vue3项目中组件切换不起作用
前端·javascript·vue.js
剑亦未配妥3 小时前
前端项目场景相关的面试题,包含验证码、图片存储、登录鉴权、动态路由、组件划分等项目场景实际的面试题
前端
先知demons3 小时前
超好用的element的el-pagination分页组件二次封装-附源码及讲解
前端·javascript·vue.js·elementui·html
emmm4593 小时前
前端DOM常用操作
前端·javascript