练习: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;
        }
        
        #control {
            width: 590px;
            height: 50px;
            border: 10px solid black;
            /* border-bottom-width: 0px; */
            background-color: gray;
            margin: 0 auto;
            text-align: center;
            line-height: 50px;
        }
        
        #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="control">
        <button id="round">循环播放</button>
        <button id="single">顺序播放</button>
    </div>
    <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 = 0;
                    alert("已经是第一张图片了!");
                } else if (index === -1 && !flag) {
                    index = imgArr.length - 1;
                }
                showData();
            }
            //右箭头事件绑定
        $('next').onclick = function() {
                index++;
                if (index === imgArr.length && flag) {
                    index = imgArr.length - 1;
                    alert("已经是最后一张了!");
                } else if (index === imgArr.length && !flag) {
                    index = 0;
                }
                showData();
            }
            //循环播放事件绑定
        $('round').onclick = function() {
            flag = false;
            alert("现在开始循环播放!");
        }
        $('single').onclick = function() {
            flag = true;
            alert("现在开始顺序播放!");
        }
    </script>
</body>

</html>

效果图
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值