前端——切换轮播图

学完前端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>
相关推荐
Justineo1 分钟前
TemPad Dev:设计与实现
前端·javascript·css
HuangYongbiao2 分钟前
Rspack 插件架构原理:从 Tapable 到 Rust Hook
前端·架构
llq_3502 分钟前
ReactDOM.createPortal 与 position: fixed 的本质区别
前端
Cache技术分享11 分钟前
231. Java 集合 - 将集合元素转换为数组
前端·后端
神秘的猪头13 分钟前
浏览器是如何渲染 HTML/CSS/JS 页面的?——从代码到像素的完整流程
前端·javascript
啷咯哩咯啷18 分钟前
el-table-v2 实现自适应列宽
前端·javascript·vue.js
jump68019 分钟前
为什么typeof null = 'object'
前端
__不想说话__19 分钟前
给网站做“体检”:Lighthouse如何平息产品经理的怒火
前端·google·架构
玉宇夕落21 分钟前
🚀 从 HTML 到像素:浏览器渲染全流程揭秘(附性能优化实战)
前端·dom
西甲甲23 分钟前
chromium UI 简要解析
前端