动画轮播图

css

 * {
            padding: 0;
            margin: 0;
        }

        #box {
            margin: 100px auto;
            width: 600px;
            height: 400px;
            position: relative;
        }

        #parent {
            width: 600px;
            height: 400px;
            border: 1px solid red;
            overflow: hidden;
        }

        #child {
            float: left;
            width: 2400px;
            height: 400px;

        }

        #child img {
            float: left;
            width: 600px;
            height: 400px;
            display: block;
        }

        #ulList {
            position: absolute;
            left: 50px;
            bottom: 20px;
        }

        #ulList ul {
            list-style: none;
            overflow: hidden;
        }

        ul li {
            float: left;
            padding: 10px;
            border: 1px solid black;
            border-right: 0;
        }

        ul li:last-child {
            border-right: 1px solid black;
        }

        /* #btn {
            position: relative;
        } */

        #leftBtn,
        #rightBtn {
            position: absolute;
            top: 150px;
            padding: 30px 10px;
            border: 1px solid black;
           
        }

        #leftBtn {
            left: 0;
        }

        #rightBtn {
            right: 0;
        }

html

 <div id="box">
        <div id="parent">
            <div id="child">
                <img src="./image/1.jpg" alt="">
                <img src="./image/2.jpg" alt="">
                <img src="./image/4.jpg" alt="">
                <img src="./image/5.jpg" alt="">
            </div>
        </div>
        <div id="ulList">
            <ul>
                <li style="background-color: red;">1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
            </ul>
        </div>
        <div id="leftBtn"></div>
        <div id="rightBtn"></div>
    </div>

js

 var parent = document.getElementById('parent')
    var childImg = document.getElementById('child').getElementsByTagName('img')[0]
    var ulList = document.getElementById('ulList').getElementsByTagName('li')
    var leftBtn = document.getElementById('leftBtn')
    var rightBtn = document.getElementById('rightBtn')
    var num = 0
    var timeAuto;
    clock()
    function clock() {
        timeAuto = setInterval(autoMove, 2000)
    }

    function autoMove() {
        num++
        if (num > 3) {
            num = 0
        }
        if (num < 0) {
            num = 3
        }
        // console.log(1);
        // parent.scrollLeft += childImg.clientWidth
        // parent.scrollLeft = childImg.clientWidth * num
        slowMove(parent.scrollLeft, childImg.clientWidth * num)
        changeColor()
        // console.log(parent.scrollLeft);
    }
    // 缓慢轮播
    function slowMove(strat, end) {
        var step = 0
        var maxSetp = 10 //假设10步走完
        var everyStep = (end - strat) / maxSetp //60 
        var timer = setInterval(function () {
            step++
            if (step <= maxSetp) {
                parent.scrollLeft += everyStep
            } else {
                clearInterval(timer)
            }
        }, 50)
    }
    // 小点颜色
    function changeColor() {
        for (var i = 0; i < ulList.length; i++) {
            ulList[i].style.backgroundColor = ''
        }
        ulList[num].style.backgroundColor = 'red'
    }
    // 小点轮播
    for (let i = 0; i < ulList.length; i++) {
        ulList[i].onclick = function () {
            console.log('点击');
            console.log(i);
            // 清空定时器
            clearInterval(timeAuto)
            num = i
            slowMove(parent.scrollLeft, childImg.clientWidth * num)
            changeColor()
            clock()
        }
    }
    // 左右轮播
    rightBtn.onclick = function () {
        console.log('rightBtn');
        clearInterval(timeAuto)
        autoMove()
        clock()
    }
    leftBtn.onclick = function () {
        console.log('leftBtn');
        clearInterval(timeAuto)
        num -= 2
        autoMove()
        clock()
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值