JS图片切换效果(低配版轮播图)

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 700px;
            height: 450px;
            margin: 50px auto;
            border: 1px solid;
        }

        .up {
            height: 30px;
            text-align: center;
            line-height: 30px;
            background-color: #fff;
        }

        .content {
            height: 390px;
            position: relative;
        }

        .content img {
            height: 390px;
            width: 700px;
        }

        .button {
            width: 100%;
            display: flex;
            position: absolute;
            top: 50%;
            margin-top: -25px;
            height: 50px;
            justify-content: space-between;
        }
        .button span {
            background: rgba(204, 204, 204, 0.5);
            cursor: pointer;
            text-align: center;
            line-height: 40px;
            font-size: 12px;
            display: block;
            height: 40px;
            width: 60px;
            color: #fff;
        }
        .button span:nth-child(1){
            border-top-right-radius: 15px;
            border-bottom-right-radius: 15px;
        }
        .button span:nth-child(2){
            border-top-left-radius: 15px;
            border-bottom-left-radius: 15px;
        }
        .down {
            background-color: #ccc;
            font-size: 18px;
            height: 30px;
            line-height: 30px;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="up"></div>
        <div class="content">
            <img src="" alt="" class="pic">
            <div class="button">
                <span class="revious">上一张</span>
                <span class="next">下一张</span>
            </div>

        </div>
        <div class="down"></div>
    </div>

    <script>

        var box = document.querySelector(".box"); //获取元素
        var up = document.querySelector(".up");
        var down = document.querySelector(".down");
        var pic = document.querySelector(".pic");
        var revious = document.querySelector(".revious")
        var next = document.querySelector(".next");
        // console.log(box,up,down,pic,revious,next);      
        //初识化
        var picSrc = ["images/1.jpg", "images/2.jpg", "images/3.jpg", "images/4.jpg", "images/5.jpg"];
        var picName = ["掌中舞罢箫声绝,三十六宫秋夜长", "唯有知情一片月,曾窥飞鸟入昭阳", "苦心膏沐不论赀,富贵人生各有时", "一枝红艳露凝香,云雨巫山枉断肠", "剧终"]
        var num = 0;
        var temer;  
        up.innerHTML = "1/5";   
        pic.src = picSrc[0];
        down.innerHTML = picName[0];
        next.onclick = function () {
            num++;
            if (num > picSrc.length - 1) {
                num = 0;
            }
            begin();       //调用函数
            // pic.innerHTML = picSrc[num];          //因和下面重复,直接用函数写,更简洁
            // up.innerHTML = num + 1 + "/" + picSrc.length;
            // down.innerHTML = picName[num];
            // pic.src = picSrc[num];
        }
        revious.onclick = function () {
            num--;
            if (num < 0) {
                num = picSrc.length - 1;
            }
            begin();
            // pic.innerHTML = picSrc[num];
            // up.innerHTML = num + 1 + "/" + picSrc.length;
            // down.innerHTML = picName[num];
            // pic.src = picSrc[num];
        }

        function begin() {          //把上面重复部分直接写成函数,然后调用
            pic.innerHTML = picSrc[num];
            up.innerHTML = num + 1 + "/" + picSrc.length;
            down.innerHTML = picName[num];
            pic.src = picSrc[num];
        }

        timer = setInterval(function () {   //设置定时器,让图片自动切换
            next.onclick();
        }, 1000);

        box.onmouseover = function () {     //当鼠标移入div中,清除定时器(停止自动切换)
            clearInterval(timer);
        }
        box.onmouseout = function () {      //当鼠标移出,再次执行定时器
            timer = setInterval(function () {
                next.onclick();
            }, 1000);
        }


    </script>
</body>

</html>
                                 **效果图如下** 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值