前端——切换轮播图

学完前端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>
相关推荐
king王一帅2 小时前
Incremark Solid 版本上线:Vue/React/Svelte/Solid 四大框架,统一体验
前端·javascript·人工智能
智航GIS7 小时前
10.4 Selenium:Web 自动化测试框架
前端·python·selenium·测试工具
前端工作日常7 小时前
我学习到的A2UI概念
前端
徐同保7 小时前
为什么修改 .gitignore 后还能提交
前端
一只小bit7 小时前
Qt 常用控件详解:按钮类 / 显示类 / 输入类属性、信号与实战示例
前端·c++·qt·gui
Mr -老鬼8 小时前
前端静态路由与动态路由:全维度总结与实践指南
前端
颜酱8 小时前
前端必备动态规划的10道经典题目
前端·后端·算法
wen__xvn9 小时前
代码随想录算法训练营DAY10第五章 栈与队列part01
java·前端·算法
大怪v9 小时前
前端佬们!!AI大势已来,未来的上限取决你的独特气质!恭请批阅!!
前端·程序员·ai编程
Mr -老鬼10 小时前
功能需求对前后端技术选型的横向建议
开发语言·前端·后端·前端框架