<html> <head> <meta charset="utf-8" /> <title></title> <script> window.onload=function(){ setInterval("rollImg()",2000); } var imgarr=["https://www.w3 ...
<html> <head> <meta charset="utf-8" /> <title></title> <script> window.onload=function(){ setInterval("rollImg()",2000); } var imgarr=["https://www.w3school.com.cn/i/eg_tulip.jpg", "https://www.w3school.com.cn/i/eg_chinarose.jpg", "https://www.runoob.com/images/pulpit.jpg"] var i = 1; function rollImg(){ i++; if(i == 4){ i = 1; } var img_id = document.getElementById("img_id"); img_id.src= imgarr[i-1]; } </script> </head> <body> <center> <div style="width: 1200px; height: 800px; border: 5px solid purple; overflow: hidden;"> <img src="https://www.w3school.com.cn/i/eg_tulip.jpg" id="img_id"/> </div> </center> </body> </html>
這是簡單的原生js圖片輪播,複製代碼保存為網頁即可看到效果。如有錯誤和不足之處,歡迎指正