原生js实现幻灯片效果

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<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>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #container {
            margin: 100px 100px;
            display: flex;
            width: 1040px;
            height: 700px;
            justify-content: space-around;
            align-items: center;
            background-color: #eee;
            border: 1px solid #ccc;
            box-shadow: #ccc 0 0 10px;
        }

        .playimg {
            width: 800px;
            height: 600px;
        }

        ul {
            list-style: none;
            width: 200px;
            height: 600px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        img {
            width: 100%;
            height: 100%;
        }

        li>img:hover {
            box-shadow: 0 0 10px #0ff;
            /* border-color: #0ff; */
            /* width: 500px;
            height: 400px; */

        }
    </style>
</head>

<body>
    <div id="container">
        <div id="play" class="playimg">
            <img id="imgPlay" src="" alt="">
        </div>
        <ul>
            <li><img class="liImg active" src="" alt=""></li>
            <li><img class="liImg" src="" alt=""></li>
            <li><img class="liImg" src="" alt=""></li>
            <li><img class="liImg" src="" alt=""></li>
        </ul>
    </div>
    <script>
        var ImgArr = [
            'images/1.jpg',
            'images/2.jpg',
            'images/3.jpg',
            'images/4.jpg'
        ];
        var divPlay = document.getElementById("play");
        var imgPlay = document.getElementById('imgPlay');
        var liImg = document.getElementsByClassName('liImg');

        var clock; //计时器
        var count = 0; //计数器


        //刚开始加载时显示第一张
        imgPlay.src = ImgArr[0];

        // 为右侧的图片绑定点击事件
        for (let i = 0; i < liImg.length; i++) {
            liImg[i].src = ImgArr[i];
            liImg[i].onclick = () => {
                imgPlay.src = ImgArr[i];
            }
            liImg[i].onmouseenter = () => {
                imgPlay.src = ImgArr[i];
                stop();
            }
            liImg[i].onmouseleave = () => {
                again();
            }
        }
        divPlay.onmouseenter = () => {
            stop();
        }
        divPlay.onmouseleave = () => {
            again();
        }
        //轮播
        function start() {
            if (count == 3) {
                count = 1;
            } else {
                count++; //更新计数器
            }
            imgPlay.src = ImgArr[count];

        }

        //停止轮播
        function stop() {
            clearInterval(clock);
        }

        //鼠标离开继续轮播
        function again() {
            clock = setInterval(this.start, 2000);
        }
        (() => {
            clock = setInterval(this.start, 2000); //创建计时器,2秒执行一次start函数
        })();
    </script>
</body>

</html>

仅学习使用

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值