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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条