本文給大家分享一段js代碼實現自動切換圖片的實例代碼,代碼如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <meta name="author" content="" /> <meta name="keywords" content="" /> <meta name="description" content="" /> <link rel="stylesheet" type="text/css" href="" /> <style type="text/css"></style> <script type="text/javascript"> window.onload=init; var n=1; //圖片標記數 var dingshi; //讓圖片動的定時器 function init(){ dingshi=window.setInterval("tupian()",1000); } //更換圖片 function tupian(){ var obj=document.getElementById("img1"); n++; if(n>=4){ n=1; } obj.src="yaj_"+n+".jpg"; } //鼠標放上圖片停止 function stoptupian(){ window.clearInterval(dingshi); } //鼠標離開圖片動起來 function dongqilai(){ dingshi=window.setInterval("tupian()",1000); } </script> </head> <body> <img src="yaj_1.jpg" onmouseover="stoptupian()" onmouseout="dongqilai()" id="img1"> </body> </html>
以上所述是小編給大家介紹的JavaScript實現自動切換圖片代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網站的支持!