javascript点击按钮循序和循环播放轮播图片

循序和循环播放轮播图片

<style>
        * {
            padding: 0;
            margin: 0;
        }

        #place{
            width: 800px;
            text-align: center;
            margin: 0 auto;
        }

        #box{   
            width: 800px;
            height: 525px;
            border: 1px solid;
            position: relative;
        }
       

        #box p{
            position: absolute;
            width: 100%;
            line-height: 30px;
            background: rgba(0,0,0,.3);
            color: #fff;
        }

        #box .taoming1{
            top: 0;
        }
        #box .taoming2{
            bottom: 0;
        }

        #box button{
            position: absolute;
            width: 35px;
            height: 50px;
            font-size: 30px;
            top: 50%;
            margin-top: -25px;
            background:rgba(0,0,0,.3);
            color: #fff;
        }
        #box .leftBut{
            left: 0;
        }
        #box .rightBut{
            right: 0;
        }
        #box img{
            width: 100%;
            height: 100%;
        }
       
    </style>




</head>

<body>
    <div id="place">
                   <button>顺序</button>
                   <button>循环</button>
        <div id="box">
            <img src="./img/1.jpg" alt="">
                <p class="taoming1">1/4</p>
                <p class="taoming2">美女1</p>
                <button class="leftBut"><</button>
                <button class="rightBut">></button>
        </div>
    </div>
</body>
<script type="text/javascript">
     var img = document.getElementsByTagName('img')[0];
     var taom = document.getElementsByTagName('p');
     var btn = document.getElementsByTagName('button');
     var mg = ['img/1.jpg','img/2.jpg','img/3.jpg','img/4.jpg'];
     var n = 0; 
     var tag = true;
    
     btn[0].onclick = function(){
         tag = true;
     }
     btn[1].onclick = function(){
         tag = false;
     }
     //点击右边的按钮,切换图片
     btn[3].onclick = function(){
         n++;
         if(n == mg.length){
             if(tag == true){
                 alert('已经是最后一张');
                 n = mg.length - 1;
             }else{
                 n=0;
             }
         }
             img.src = mg[n];
             taom[0].innerHTML = n+1+"/4";
            taom[1].innerHTML = "美女"+(n+1);
     }
// 点击左边的按钮,切换图片
  btn[2].onclick = function(){
    n--;
    if(n < 0){
        if(tag == true){
            alert('已经是最前一张');
            n=0;
        }else{
            n=mg.length - 1;
        }
    }
            img.src = mg[n];
             taom[0].innerHTML = n+1+"/4";
            taom[1].innerHTML = "美女"+(n+1);
}

</script>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值