JS小练————轮播图(含注释,运行图)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/缓动(回调).js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .all {
            width: 400px;
            height: 225px;
            /* border: 1px solid red; */
            position: relative;
            top: 150px;
            left: 400px;
            overflow: hidden;
        }

        ul {
            width: 400%;
            height: 200px;
            background-color: antiquewhite;
            position: absolute;
            top: 0px;
            left: 0px;
            z-index: 1;
        }

        img {
            width: 400px;
            float: left;
        }

        button {
            height: 100px;
            width: 80px;
            z-index: 2;
            position: relative;
            opacity: .3;
        }

        .b-left {
            top: 63px;
            display: none;
        }

        .b-right {
            top: -30px;
            left: 320px;
            display: none;
        }

        .yuan {
            height: 50px;
            /* background-color: aqua; */
            width: 300px;
            position: absolute;
            top: 190px;
            left: 50px;
            text-align: center;
            line-height: 50px;
            z-index: 2;
            display: flex;
            justify-content: center;
            flex-direction: row;
            align-items: center;
        }

        .yuan div {
            height: 10px;
            width: 10px;
            border-radius: 50%;
            background-color: rgba(273, 273, 283, .8);
            margin-left: 5px;
            cursor: pointer;
        }

        .yuan .red {
            background-color: red;
        }
    </style>
</head>

<body>
    <!-- <div class="red"></div> -->
    <div class="all">
        <button class="b-left">&lt;</button>
        <button class="b-right">&gt;</button>
        <ul>
            <li><img src="../img/1.png" alt=""></li>
            <li><img src="../img/2.jpg" alt=""></li>
            <li><img src="../img/4.jpg" alt=""></li>
            <li><img src="../img/3.jpg" alt=""></li>
            <!-- <li><img src="../img/1.png" alt=""></li> -->
            <li><img src="../img/1.png" alt=""></li>
        </ul>
        <div class="yuan">

        </div>
    </div>
    <script>
        let all = document.querySelector(".all")
        let ul = document.querySelector("ul")
        let li = document.querySelectorAll("li")
        let bl = document.querySelector(".b-left")
        let br = document.querySelector(".b-right")
        let yuan = document.querySelector(".yuan")
        // 引入元素
        let num = 0
        // 图片的位置
        let yd = 0
        // 小圆点的位置
        ul.style.width = li.length * 400 + "px"
        // 设置ul宽度,随图片多少而变化
        all.addEventListener("mouseenter", function () {
            // 鼠标经过事件
            bl.style.display = "block"
            br.style.display = "block"
            // 左右按钮显示
            clearInterval(auto)
            // 清除定时器
            auto = null
        })
        all.addEventListener("mouseleave", function () {
            // 鼠标离开事件
            bl.style.display = "none"
            br.style.display = "none"
            // 左右按钮隐藏
            auto = setInterval(function () {
                br.click()
            }, 2000)
            // 调用定时器
        })
        br.addEventListener("click", function () {
            if (num == li.length - 1) {
                ul.style.left = 0 + "px"
                num = 0
                // 如果到最后一页,点击右按钮回到第一页,重置num
            }
            num++
            animation(ul, -400 * num)
            // 调用缓动函数
            yd++
            if (yd == li.length - 1) {
                yd = 0
                // 如果小圆点到最后,则点击调回第一张
            }
            yuanmove()
            // 调用圆点移动函数
        })
        bl.addEventListener("click", function () {
            // 左按钮点击事件
            if (num == 0) {
                num = li.length - 1
                ul.style.left = -num * 400 + "px"
                // 如果在第一张,点击左按钮则跳转最后一张
            }
            num--
            animation(ul, -400 * num)
            // 调用缓动函数
            yd--
            yd = yd < 0 ? yuan.children.length - 1 : yd
            // 若小圆点位置为负数则变为最后一张位置
            yuanmove()
        })
        for (i = 0; i < li.length - 1; i++) {
            let div = document.createElement("div")
            // 创建div在yuan内
            div.setAttribute("index", i)
            // 给div编写编号
            yuan.appendChild(div)
            // 将div加入yuan中
            yuan.children[0].className = "red"
            // 默认第一个圆点为红色
            div.addEventListener("click", function () {
                // 圆点点击事件
                for (j = 0; j < li.length - 1; j++) {
                    yuan.children[j].className = " "
                    // 循环清除圆点样式
                }
                this.className = "red"
                // 点击的圆点赋予属性
                let index = this.getAttribute("index")
                // 获取index
                num = index
                // index赋予num
                yd = index
                // index赋予yd
                animation(ul, -index * 400)
                // 调用缓动
            })
        }
        function yuanmove() {
            // 圆点移动函数
            for (i = 0; i < yuan.children.length; i++) {
                yuan.children[i].className = ""
                // 遍历清空样式
            }
            yuan.children[yd].className = "red"
            // 当前赋予样式
        }
        let auto = setInterval(function () {
            // 自动播放定时器
            br.click()
            // 自动点击右按钮
        }, 2000)
    </script>
</body>
</html>

 

 

  • 44
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 45
    评论
好的,以下是制作网页轮播图注释的步骤: 1. 首先,在 HTML 文件中创建一个 div 元素,用于包轮播图: ```html <div id="slideshow"></div> ``` 2. 在 CSS 文件中设置轮播图容器的宽度和高度,并设置 overflow 属性为 hidden,以隐藏超出容器范围的内容: ```css #slideshow { width: 600px; height: 400px; overflow: hidden; } ``` 3. 在 JavaScript 文件中创建一个数组,包要显示的图片的路径。接着,创建一个变量来追踪当前显示的图片的索引: ```javascript var images = [ "image1.jpg", "image2.jpg", "image3.jpg" ]; var currentIndex = 0; ``` 4. 创建一个函数,用于在轮播图容器中显示下一张图片。该函数应该更新当前索引并将当前图片显示在轮播图容器中: ```javascript function nextImage() { currentIndex++; if (currentIndex >= images.length) { currentIndex = 0; } document.getElementById("slideshow").style.backgroundImage = "url('" + images[currentIndex] + "')"; } ``` 5. 使用 setInterval 函数调用上一步中创建的函数,以每隔一段时间自动切换图片: ```javascript setInterval(nextImage, 3000); // 切换间隔为 3 秒 ``` 6. 最后,可以在 HTML 文件中添加注释,以便更好地理解代码的作用: ```html <!-- 轮播图容器 --> <div id="slideshow"></div> <!-- CSS 文件 --> <style> #slideshow { width: 600px; height: 400px; overflow: hidden; } </style> <!-- JavaScript 文件 --> <script> // 图片路径数组 var images = [ "image1.jpg", "image2.jpg", "image3.jpg" ]; // 当前图片索引 var currentIndex = 0; // 显示下一张图片的函数 function nextImage() { currentIndex++; if (currentIndex >= images.length) { currentIndex = 0; } document.getElementById("slideshow").style.backgroundImage = "url('" + images[currentIndex] + "')"; } // 自动切换图片 setInterval(nextImage, 3000); // 切换间隔为 3 秒 </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 45
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值