romancekami 2022-03-30 19:21 采纳率: 100%
浏览 32
已结题

js轮播图无法正常填充轮播区域,后面的图会跑上来

从网上找了一份轮播图的代码来用,源码容纳轮播图的

的宽度width是1226px定死的,但我这边因为轮播图是作为网页中的一部分,同时希望兼顾宽屏显示所以宽度width是给的60%,我把js里的往左移动的代码改成了oImgList.style.left = index * -100 + "%"。
但是最后轮播的效果不对(轮播的区域没法正常显示一张图,会有部分后面的图片),我怀疑是不是自己的几张图大小是不完全一样的,所以导致了这个问题?(我自己看了css改了一点结果也不太行,可能是我太菜了,来问问大家可以怎么解决。)

img

我更改后的html和js代码如下:

<!DOCTYPE html>
<html lang="zh-cn">
<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>轮播图</title>
    <link rel="stylesheet" href="lunbotu.css">
    <script>
        window.addEventListener("load",function() {
            // 获取左右按钮和图片列表
            let oLeft = document.querySelector(".left");
            let oRight = document.querySelector(".right");
            let oImgList = document.querySelector(".img-list");

            // 克隆第一张图片
            let clonefirstImg = oImgList.firstElementChild.cloneNode();
            // 将第一张图片添加至图片列表的末尾
            oImgList.appendChild(clonefirstImg);

            // 图片索引 代表当前是第几张图片  index:0代表第一张图片
            let index = 0;

            // 设置函数节流锁
            let lock = true;
            function handleRightBtn() {
                if (!lock) return;
                index++;
                oImgList.style.left = index * -100 + "%";
                // 为什么要加过渡? 因为切换到了最后一张假图时会将过渡去掉
                oImgList.style.transition = "0.5s ease";

                if (index === 5) {
                index = 0;
                setTimeout(() => {
                    oImgList.style.left = 0;
                    // 取消过渡 500毫秒之后切换第一张
                    oImgList.style.transition = "none";
                }, 500);
                }

                // 设置小圆点的高亮
                setCircles();
                // 关锁
                lock = false;
                setTimeout(() => {
                lock = true;
                }, 500);
            }

            // 右按钮的实现
            oRight.addEventListener("click", handleRightBtn);

            // 左按钮的实现  瞬间切换到假图然后拉到真实最后一张图片
            oLeft.addEventListener("click", () => {
                if (!lock) return;
                index--;
                if (index === -1) {
                oImgList.style.left = 5 * -100 + "%";
                oImgList.style.transition = "none";
                index = 4;
                setTimeout(() => {
                    oImgList.style.left = index * -100 + "%";
                    oImgList.style.transition = "0.5s ease";
                }, 0);
                } else {
                oImgList.style.left = index * -100 + "%";
                }

                // 设置小圆点的高亮
                setCircles();

                lock = false;
                setTimeout(() => {
                lock = true;
                }, 500);
            });

            // 获取五个小圆点
            const circles = document.querySelectorAll(".circle");

            // 小圆点高亮的显示
            function setCircles() {
                for (let i = 0; i < circles.length; i++) {
                if (i === index) {
                    circles[i].classList.add("active");
                } else {
                    circles[i].classList.remove("active");
                }
                }
            }

            // 小圆点点击切换图片 使用事件代理
            const oCircle = document.querySelector(".circle-list");
            oCircle.addEventListener("click", (e) => {
                // 当我点击小圆点的时候
                if (e.target.nodeName.toLowerCase() === "li") {
                // 当前元素的data-n对应得值 和index一一对应
                const n = Number(e.target.getAttribute("data-n"));
                index = n;
                setCircles();
                oImgList.style.transition = "0.5s ease";
                oImgList.style.left = index * -100 + "%";
                }
            });

            // 自动轮播
            let autoplay = setInterval(handleRightBtn, 2000);
            const oWrap = document.getElementById("wrap");
            // 移入停止轮播
            oWrap.addEventListener("mouseenter", () => {
                clearInterval(autoplay);
            });
            // 移出继续轮播
            oWrap.addEventListener("mouseleave", () => {
                // 设表先关
                clearInterval(autoplay);
                autoplay = setInterval(handleRightBtn, 2000);
            });
        })      
    </script>
</head>
<body>
    <!-- 轮播图 -->
    <div id="wrap">
        <div class="img-list">
            <img src="https://markdowntuchuang.oss-cn-shanghai.aliyuncs.com/picture/202203301016900.png"/>
            <img src="https://markdowntuchuang.oss-cn-shanghai.aliyuncs.com/picture/202203301016729.jpeg" />
            <img src="https://markdowntuchuang.oss-cn-shanghai.aliyuncs.com/picture/202203301017505.jpg" />
            <img src="https://markdowntuchuang.oss-cn-shanghai.aliyuncs.com/picture/202203301016872.jpeg" />
            <img src="https://markdowntuchuang.oss-cn-shanghai.aliyuncs.com/picture/202203301017454.jpg"/>
        </div>

        <!-- 小箭头 -->
        <div class="arrow">
            <a href="javascript:;" class="left">&gt;</a>
            <a href="javascript:;" class="right">&lt;</a>
        </div>

        <!-- 小圆点 -->
        <ul class="circle-list">
            <li class="circle active" data-n="0"></li>
            <li class="circle" data-n="1"></li>
            <li class="circle" data-n="2"></li>
            <li class="circle" data-n="3"></li>
            <li class="circle" data-n="4"></li>
        </ul>
    </div>
</body>
</html>

css的代码如下:

/* 轮播图 */
* {
    margin: 0;
    padding: 0;
}
#wrap ul {
    list-style: none;
}
#wrap {
    display: flex;
    position: relative;
    background-color: rgb(109, 114, 112);
    width: 60%;
    height: 400px;
    margin-right: 1.4%;
    margin-left: 1.4%;
    margin: auto;
    /* 空间有剩余时扩大轮播图填充剩余 */
    flex-grow: 1;
    overflow: hidden;
}
#wrap .img-list {
    display: flex;
    position: relative;
    left: 0px;
    width: 100%;
    height: 100%;
    transition: 0.5s ease;
}

#wrap .img-list img{
    width: 100%;
    cursor: pointer;
}

#wrap a {
    position: absolute;
    top: 50%;
    transform: translate(0, -50%);
    display: block;
    width: 40px;
    height: 70px;
    background-color: rgba(0, 0, 0, 0.7);
    color: white;
    user-select: none;
    font-size: 30px;
    text-align: center;
    line-height: 70px;
    text-decoration: none;
}

#wrap a.left {
    left: 0;
}

#wrap a.right {
    right: 0;
}

.circle-list {
    display: flex;
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translate(-50%, 0);
    width: 240px;
    height: 40px;
    z-index: 8;
}
.circle-list > .circle {
    margin: 0 5px;
    width: 30px;
    height: 30px;
    background-color: #ecf0f1;
    border-radius: 50%;
}

.circle-list > .circle.active {
    background-color: #e74c3c;
}

容纳轮播图的div我设置的宽度是60%,难倒这个代码只适合定死的px值并且几张图片的大小都要完全一样吗?想知道造成我这种情况的原因,希望朋友们给点建议或者解决方法呀!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 4月7日
    • 创建了问题 3月30日

    悬赏问题

    • ¥15 同一个浏览器打开两个窗口怎么区分会话
    • ¥100 如何编写自己的emmc镜像
    • ¥15 starccm线性内聚力模型
    • ¥15 点云四边形凸包确定顶点
    • ¥15 关于redhat虚拟机系统新建卷的问题
    • ¥50 WRFDA读取风云四号A 星的GIIRS数据
    • ¥15 C# 爬虫融通金网址实时银价
    • ¥15 热敏电阻NTC,温控不同颜色的LED的亮与灭,PCB
    • ¥20 ESP32使用MicroPyhon开发,怎么获取485温湿度的值,温湿度计使用的鞋子是Modbus RTU
    • ¥50 苹果MGIE项目部署缺少emb权重