练习:js实现简易的轮播图效果

制作简易的轮播图效果

<!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>
        a {
            text-decoration: none;
            color: white;
        }
        
        #container {
            width: 590px;
            height: 470px;
            margin: 0 auto;
            position: relative;
            border: 10px solid black;
            background-color: grey;
            margin-top: 10px;
        }
        
        #pre,
        #next {
            position: absolute;
            width: 40px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            font-size: 22px;
            top: 220px;
            background-color: black;
            opacity: 0.5;
        }
        
        #pre {
            left: 0;
        }
        
        #next {
            right: 0;
        }
        
        #order,
        #info {
            /* width: 590px; */
            width: 100%;
            height: 40px;
            line-height: 40px;
            text-align: center;
            background-color: black;
            opacity: 0.5;
            position: absolute;
            color: white;
        }
        
        #order {
            top: 0;
        }
        
        #info {
            bottom: 0;
        }
    </style>
</head>

<body>
    <div id="container">
        <a href="javascript:" id="pre">&lt;</a>
        <a href="javascript:" id="next">&gt;</a>
        <div id="order">图片正在加载...</div>
        <div id="info">图片正在加载...</div>
        <img src="" alt="" id="picture">
    </div>

    <script>
        //定义图片数组
        let imgArr = ['1.jpg', '2.jpg', '3.jpg', '4.jpg'];
        //定义info数组
        let infoArr = ['图片一', '图片二', '图片三', '图片四'];
        var index = 0; //数组索引
        let flag = true; //假定true为顺序播放

        function $(id) {
            return document.getElementById(id);
        }
        //定义函数,显示信息
        function showData() {
            $('order').innerHTML = (index + 1) + '/' + imgArr.length;
            $('info').innerHTML = infoArr[index];
            $('picture').src = imgArr[index];
        }
        showData();
        //左箭头事件绑定
        $('pre').onclick = function() {
                index--;
                if (index === -1 && flag) {
                    index = imgArr.length - 1;
                }
                showData();
            }
            //右箭头事件绑定
        $('next').onclick = function() {
                index++;
                if (index === imgArr.length && flag) {
                    index = 0;
                }
                showData();
            }
            //定时器 实现轮播
        setInterval(function() {
            index++;
            if (index === imgArr.length && flag) {
                index = 0;
            }
            showData();
        }, 3000);
    </script>
</body>

</html>

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值