6、轮播图案例

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #box {
            position: relative;
            margin: 30px auto;
            width: 400px;
            height: 450px;
            border: 10px dotted red;
        }
        #parent {
            width: 400px;
            height: 300px;
            border: 2px solid blue;
            overflow: hidden;
        }
        #parent img {
            float: left;
            width: 400px;
            height: 300px;
        }
        ul {
            width: 400%;
            height: 400px;
            list-style: none;
        }
        ul>li {
            float: left;
        }
        #list {
            position: absolute;
            top: 250px;
            left: 100px;
        }
        #list>li {
            margin: 0 5px;
            width: 30px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            border: 1px solid black;
            background-color: white;
        }
        #left {
            top: 130px;
            left: 0;
            width: 30px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            background-color: aquamarine;
            position: absolute;
        }
        #right {
            top: 130px;
            right: 0;
            width: 30px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            background-color: aquamarine;
            position: absolute;
        }
    </style>
</head>

<body>
    <div id="box">
        <!-- 轮播图 -->
        <div id="parent">
            <ul>
                <li><img src="./images/1.jpg" alt=""></li>
                <li><img src="./images/2.jpg" alt=""></li>
                <li><img src="./images/3.jpg" alt=""></li>
                <li><img src="./images/4.jpg" alt=""></li>
            </ul>
        </div>
        <!-- 四个小点 -->
        <ul id="list">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
        <!-- 左按钮 -->
        <div id="left">左</div>
        <!-- 右按钮 -->
        <div id="right">右</div>
    </div>
</body>
<script>

    var left = document.getElementById("left")
    var right = document.getElementById("right")
    var liObj = document.querySelectorAll("#list>li")

    var timer
    var num = 0
    // 1、自动轮播
    var parent = document.getElementById("parent")
    var imgObj = document.querySelectorAll("#parent img")[0]
    auto()
    function auto() {
        timer = setInterval(move, 2000)
    }
    function move() {
        num++
        if (num > 3) {
            num = 0
        }
        if (num < 0) {
            num = 3
        }
        // parent.scrollLeft = imgObj.clientWidth * num
        slow(parent.scrollLeft, imgObj.clientWidth * num)
        changeColor()
    }

    // 2、缓慢移动
    function slow(start, end) {
        var maxStep = 10
        var step = 0
        var every = (end - start) / maxStep
        var slowMove = setInterval(function () {
            step++
            if (step <= maxStep) {
                parent.scrollLeft = every + parent.scrollLeft
            } else {
                clearInterval(slowMove)
            }
        }, 50)
    }
    // 3、点击四个小点轮播
    for (var i = 0; i < liObj.length; i++) {
        liObj[i].onclick = function () {
            clearInterval(timer)
            for (var j = 0; j < liObj.length; j++) {
                if (liObj[j] == this) {
                    num = j
                    slow(parent.scrollLeft, imgObj.clientWidth * j)
                    changeColor()
                }
            }
            auto()
        }
    }
    // 4、小点点击变颜色
    function changeColor() {
        for (var i = 0; i < liObj.length; i++) {
            liObj[i].style.backgroundColor = "white"
        }
        liObj[num].style.backgroundColor = "pink"
    }
    // 5、单击右按钮
    right.onclick = function () {
        clearInterval(timer)
        move()
        auto()
    }
    // 6、单击左按钮
    left.onclick = function () {
        clearInterval(timer)
        num -= 2
        move()
        auto()
    }
</script>
</html>
相关推荐
变与不变8064 小时前
jQuery怎么使用
前端·javascript
神膘护体小月半5 小时前
flex 布局的 order 属性
前端·css
paopaokaka_luck5 小时前
流浪动物救助与收养系统(DeepSeek AI对话、 ECharts多维分析、救助流程衔接、领养与回访衔接、 捐赠确认与公示、志愿者协作)
java·前端·javascript·人工智能·mysql·echarts·推荐算法
仰望.5 小时前
vue 表格组件 vxe-table 树表格懒加载支持拖拽排序的详细用法
前端·javascript·vue.js·vxe-table
前端 贾公子5 小时前
vite-plugin-uni-manifest 升级指南
前端
小码哥_常5 小时前
iPhone 用户传了张 HEIC,我的前端崩了
前端
小码哥_常5 小时前
你发的原图可能暴露你家地址——聊聊图片 EXIF 隐私
前端
java1234_小锋5 小时前
【技术专题】CSS3 网页技术 - CSS3 优先级
前端·css·css3
鸿蒙开发6 小时前
共享 Schema 与 SchemaFactory
前端·后端·harmonyos
屏什么录6 小时前
录屏超过两小时就卡顿崩盘?从参数调优到代码实现,一文彻底搞定长时间录屏
前端·音视频开发