js轮播图

1.此轮播图用原生js写的,内容过长。

2.完整版原生js轮播图

代码实现后展示:

 

3.图片自行更换自己喜欢的

4.复制时图片格式建议设成这种:

 

全部代码:

<!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>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .box {
            position: relative;
            width: 800px;
            height: 400px;
            border: 1px solid;
            margin: 50px auto;
        }

        .box .view,
        .box .view img {
            width: 100%;
            height: 100%;
        }

        .box button {
            position: absolute;
            top: 150px;
            left: 20px;
            width: 50px;
            height: 100px;
            background-color: #cccccc5b;
            border: none;
            font-size: 30px;
            color: #cccccc81;
        }

        .box .rt {
            left: calc(100% - 70px);
        }

        .box:hover button {
            background-color: #cccccc6c;
            color: #999;
        }

        .box ul {
            display: flex;
            justify-content: center;
            position: absolute;
            top: 330px;
            width: 100%;
            height: 30px;
        }

        .box ul li {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            border: 2px solid #cccccc;
            margin: 0 25px;
            background-color: #000;
        }

        .box ul li:hover {
            border-color: #666;
            background-color: #ccc;
        }
    </style>
</head>

<body>
    <section class="box">
        <div class="view">
            <img src="" alt="images">
        </div>
        <button class="lt">&lt;</button>
        <button class="rt">&gt;</button>
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </section>

</body>
<script>
    // 要求:上一页点击切换,下一页点击切换,圆点点击 “对应的图片显示”
    // 1.获取元素(图片,左右按钮,圆点)
    var img = document.querySelector('img');
    var ltBtn = document.querySelector('.lt');
    var rtBtn = document.querySelector('.rt');
    var circle = document.querySelectorAll('li');
    // 2.初始化图片和圆点样式
    var arr = ['1.jpg', '2.jpg', '3.jpg', '4.jpg']; //图片数组
    var count = 0; //代表下标“初始化下标”
    img.src = "../2022.06.07/img/" + arr[count]; 
    circle[count].style.backgroundColor = "red";
    // 3.添加事件
    // 上一张
    ltBtn.onclick = function () {
        count--;
        if (count < 0) {
            count = 3;
        }
        // 清除所有的圆点样式
        for (var i = 0; i < circle.length; i++) {
            circle[i].style.backgroundColor = "#000";
        }
        img.src = "../2022.06.07/img/" + arr[count];
        circle[count].style.backgroundColor = "red";
    }
    // 下一张
    rtBtn.onclick = function () {
        count++;
        if (count == 4) {
            count = 0;
        }
        // 清除所有的圆点样式
        for (var i = 0; i < circle.length; i++) {
            circle[i].style.backgroundColor = "#000";
        }
        img.src = "../2022.06.07/img/" + arr[count];
        circle[count].style.backgroundColor = "red";
    }
    // 圆点(事件,跟随)
    Array.from(circle).forEach(function (item, index, circle) {
        item.onclick = function () {
            // 清除所有的圆点样式
            for (var i = 0; i < circle.length; i++) {
                circle[i].style.backgroundColor = "#000";
            }
            img.src = "../2022.06.07/img/" + arr[index];
            circle[index].style.backgroundColor = "red";
        }
    });
</script>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值