轮播图制作(1)

轮播图制作

<body>
    <div>
        <img src="img/1.jpg" class="imgs" alt="">
        <a href="#" class="left"><</a> //此处的箭头也可以用图标做出来
        <a href="#" class="right">></a>
        <ul>
            <li class="col"></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
    </div>
</body>
 <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }
        
        div {
            width: 500px;
            height: 300px;
            background-color: cadetblue;
            margin: 100px auto;
            position: relative;
            cursor: pointer;
        }
        
        div:hover a {
            display: block;
        }
        
        img {
            width: 100%;
            height: 100%;
        }
        
        div ul {
            width: 100px;
            height: 20px;
            position: absolute;
            display: flex;
            justify-content: space-around;
            bottom: 10px;
            left: 50%;
            border-radius: 10px;
            transform: translateX(-50%);
            /* background-color: rgba(155, 154, 154, 0.7); */
        }
        
        div ul li {
            float: left;
            list-style-type: none;
            width: 10px;
            height: 10px;
            margin-top: 5px;
            background-color: #fff;
            border-radius: 50%;
        }
        
        a {
            display: inline-block;
            display: none;
            width: 30px;
            height: 50px;
            text-decoration: none;
            color: #fff;
            font-size: 30px;
            font-weight: 200;
            line-height: 50px;
            text-align: center;
            background-color: rgba(171, 172, 170, 0.5);
        }
        
        .left {
            position: absolute;
            left: 0px;
            top: 50%;
            transform: translateY(-50%);
        }
        
        .right {
            position: absolute;
            right: 0px;
            top: 50%;
            transform: translateY(-50%);
        }
        
        .col {
            background-color: rgb(61, 115, 216);
        }
    </style>
<script>
    window.onload = function() {
        var div = document.querySelector('div');
        var img = document.querySelector('.imgs');
        var la = document.querySelector('.left');
        var ra = document.querySelector('.right');
        var li = document.querySelectorAll('li');
        var i = 1;
        var timer = null;


        function imgs() {
            i++;
            if (i > 6) i = 1;
            img.src = "img/" + i + ".jpg";
            for (var j = 0; j < li.length; j++) {
                li[j].className = '';
                li[i - 1].className = 'col';
            }
        }
        timer = setInterval(imgs, 2500);

        // 移入暂停
        div.addEventListener('mouseover', function() {
            clearInterval(timer);
        });
        div.addEventListener('mouseout', function() {
            timer = setInterval(imgs, 2500);
        });

        // 左右
        ra.addEventListener('click', imgs);
        la.addEventListener('click', function() {
            i--;
            if (i < 1) i = 6;
            img.src = "img/" + i + ".jpg";
            for (var j = 0; j < li.length; j++) {
                li[j].className = '';
                li[i - 1].className = 'col';
            }
        });
        // 小圆点
        for (var j = 0; j < li.length; j++) {
            li[j].index = j; //添加下标
            li[j].addEventListener('click', function() {
                for (var k = 0; k < li.length; k++) {
                    li[k].className = '';
                }
                console.log(this.index);
                li[this.index].className = 'col';
                i = this.index + 1;
                img.src = "img/" + i + ".jpg";
            })
        }
    }
</script>

实现的效果如下:

在这里插入图片描述

images

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑白两客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值