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>
        .box{
            position: relative;
        }
        img{
            width: 300px;
            height:200px;
        }
        ul{
            position: absolute;
            bottom:0;
            padding: 0;
            margin: 0;
            width:300px;
            height:30px;
            background-color:  red;
        }
        ul p{
            position: absolute;
            color:gold;

            bottom:30px;
            font-size:12px;
            margin: 0;
            padding:0;
            padding-left:10px;
            width:290px;
            background-color: rgba(160, 160, 160, 0.575);
        }
        ul li{
            display: inline-block;
            width:10px;
            height:10px;
            /* background-color: rgba(0, 0, 0, 0); */
            border-radius:50%;
            margin-top:15px;
            margin-left:10px;
            border:1px solid white;
            box-sizing: border-box;
        }
        .whiteDot{
            display: inline-block;
            width:10px;
            height:10px;
            background-color: rgb(255, 255, 255); 
            border-radius:50%;
            margin-top:15px;
            margin-left:10px;
            border:1px solid white;
            /* box-sizing: border-box; */
        }
    </style>
</head>
<body>
    <div class="box">
        <img src="./1.jpg" alt="">
        <ul>
            <p>森林</p>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>


    <script>
       // 轮播图数组
        const sliderData = [
            {url:'1.jpg',title:'森林',color:'yellow'},
            {url:'2.jpg',title:'长城',color:'yellow'},
            {url:'3.jpg',title:'湖泊',color:'yellow'}
        ];

         // 随机数
        const random = parseInt(Math.random()*sliderData.length);
        // 获取图片元素
        const img = document.querySelector('.box img');
        img.src = sliderData[random].url;
        // 获取文字元素
        const p = document.querySelector('.box ul p');
        p.innerHTML = sliderData[random].title;
        // 获取小圆圈元素
        const li =  document.querySelector(`.box ul li:nth-child(${random+2})`);
        li.classList.add('whiteDot');
        
    </script>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值