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>
相关推荐
漂流瓶jz1 小时前
Webpack中各种devtool配置的含义与SourceMap生成逻辑
前端·javascript·webpack
前端架构师-老李1 小时前
React 中 useCallback 的基本使用和原理解析
前端·react.js·前端框架
musenh2 小时前
css样式学习
css·学习·css3
木易 士心2 小时前
CSS 中 `data-status` 的使用详解
前端·css
明月与玄武2 小时前
前端缓存战争:回车与刷新按钮的终极对决!
前端·缓存·回车 vs 点击刷新
牧马少女2 小时前
css 画一个圆角渐变色边框
前端·css
zy happy2 小时前
RuoyiApp 在vuex,state存储nickname vue2
前端·javascript·小程序·uni-app·vue·ruoyi
小雨青年3 小时前
Cursor 项目实战:AI播客策划助手(二)—— 多轮交互打磨播客文案的技术实现与实践
前端·人工智能·状态模式·交互
小光学长3 小时前
基于Vue的儿童手工创意店管理系统as8celp7(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库·vue.js
meichaoWen3 小时前
【Vue】Vue框架的基础知识强化
前端·javascript·vue.js