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>
相关推荐
GISer_Jing2 分钟前
[总结篇]个人网站
前端·javascript
lljss20208 分钟前
html文字红色粗体,闪烁渐变动画效果,中英文切换版本
css·html·css3
疯狂的沙粒23 分钟前
在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?
前端·uni-app·html
小妖66627 分钟前
html 滚动条滚动过快会留下边框线
前端·html
heroboyluck41 分钟前
Svelte 核心语法详解:Vue/React 开发者如何快速上手?
前端·svelte
海的诗篇_42 分钟前
前端开发面试题总结-JavaScript篇(二)
开发语言·前端·javascript·typescript
琹箐1 小时前
ant-design4.xx实现数字输入框; 某些输入法数字需要连续输入两次才显示
前端·javascript·anti-design-vue
程序员-小李1 小时前
VuePress完美整合Toast消息提示
前端·javascript·vue.js
Uyker2 小时前
从零开始制作小程序简单概述
前端·微信小程序·小程序
EndingCoder6 小时前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架