qq_45952708 2019-12-02 21:31 采纳率: 0%
浏览 570

js轮播图,能正常自动轮播,有两个按钮,代表上一张图和下一站图,下一张图没问题,问题:点上一张图时,仍然显示下一张的图.

<style type="text/css">
    *{
        margin:0;
        padding:0;
    }
    .rota-img{
        width:650px;
        height:350px;
        position:absolute;
        top:0;right:0;bottom:0;left:0;
        margin:auto;
    }
    .rota-img button:nth-child(2){
        position:absolute;
        top:0;bottom:0;
        margin:auto 0;
        left:0;
        width:40px;
        height:40px;
        font-size:30px;
    }
    .rota-img button:nth-child(3){
        width:40px;
        height:40px;
        position:absolute;
        top:0;bottom:0;
        margin:auto 0;
        right:0;
        font-size:30px;
    }
    .rota-img img{
        width:650px;
        height:350px;
    }
  </style>
 </head>
 <body>

    <div class="rota-img">
        <img src="1.jpg" id="rotaImg" class="imgs">
        <button onclick="goPre()">&lt;</button>
        <button onclick="goNext()">&gt;</button>
    </div> 
  <script type="text/javascript">
    var arr = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg"];
    var timer;
    var inx = 0;
    function autoRotate(){
        document.getElementById("rotaImg").src = arr[inx];
        if(inx == 4){
            inx = 0;
        }else{
            inx++;
        }
        timer = setTimeout(autoRotate,2000);
    }
    autoRotate();
    function goNext(){
        clearTimeout(timer);
        document.getElementById("rotaImg").src = arr[(inx+1)];
        autoRotate();
    }
    function goPre(){
        clearTimeout(timer);
        document.getElementById("rotaImg").src = arr[(inx-1)];
        autoRotate();
    }

  </script>
  • 写回答

3条回答

  • zqbnqsdsmd 2019-12-02 22:39
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作