当前位置 : 主页 > 网络编程 > JavaScript >

js数组实现图片自动轮播

来源:互联网 收集:自由互联 发布时间:2021-07-03
!DOCTYPE html html lang="en" head meta charset="UTF-8" title使用数组实现图片自动轮播/title style type="text/css" #main{ width: 700px; height: 450px; margin: 0 auto; text-align: center; } /style script type="text/javascript" var
 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
  <meta charset="UTF-8"> 
  <title>使用数组实现图片自动轮播</title> 
  <style type="text/css">
    #main{
      width: 700px;
      height: 450px;
      margin: 0 auto;
      text-align: center;
    }
  </style>
  <script type="text/javascript"> 
    var curIndex = 0; 
    var timeInterval = 1000; 
    var arr = new Array(); 
    arr[0] = "1.png"; 
    arr[1] = "2.png"; 
    arr[2] = "3.png"; 
    arr[3] = "4.png"; 
    arr[4] = "5.png"; 
    setInterval(changeImg,timeInterval); 
    function changeImg() { 
       var obj = document.getElementById("imge"); 
       if (curIndex == arr.length-1) { 
         curIndex = 0; 
       } else { 
         curIndex += 1; 
       } 
       obj.src = arr[curIndex]; 
    } 
  </script> 
</head> 
<body> 
  <div id="main">
      <h1>使用数组实现图片自动轮播</h1>
      <img id = "imge" src = "1.png" alt="picture" /> 
  </div>
</body> 
</html>

网友评论