前端——切换轮播图

学完前端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>
相关推荐
Bellafu66621 分钟前
selenium 常用xpath写法
前端·selenium·测试工具
blackorbird3 小时前
Edge 浏览器 IE 模式成攻击突破口:黑客借仿冒网站诱导攻击
前端·edge
谷歌开发者4 小时前
Web 开发指向标 | Chrome 开发者工具学习资源 (一)
前端·chrome·学习
名字越长技术越强4 小时前
Chrome和IE获取本机ip地址
前端
天***88964 小时前
Chrome 安装失败且提示“无可用的更新” 或 “与服务器的连接意外终止”,Chrome 离线版下载安装教程
前端·chrome
半梦半醒*4 小时前
zabbix安装
linux·运维·前端·网络·zabbix
清羽_ls5 小时前
React Hooks 核心规则&自定义 Hooks
前端·react.js·hooks
你的人类朋友5 小时前
“签名”这个概念是非对称加密独有的吗?
前端·后端·安全
西陵5 小时前
Nx带来极致的前端开发体验——任务缓存
前端·javascript·架构
10年前端老司机6 小时前
Promise 常见面试题(持续更新中)
前端·javascript