本文實例講述了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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>圖片旋轉</title> <style type="text/css" > #div1{ position:relative;height:800px; border:1px solid red;} #div1 img{ position:absolute;} </style> </head> <body > <div id="div1" > <img src="http://www.jb51.net/images/logo.gif" /> <img src="http://www.jb51.net/images/logo.gif" /> <img src="http://www.jb51.net/images/logo.gif" /> <img src="http://www.jb51.net/images/logo.gif" /> <img src="http://www.jb51.net/images/logo.gif" /> <img src="http://www.jb51.net/images/logo.gif" /> </div> <script type="text/javascript" > var centerx = 400; //圓心X var centery = 300; //圓心Y var r = 300; //半徑 var oimages = document.getElementById("div1").getElementsByTagName("IMG"); //圖片集合 var cnt = oimages.length; //圖片數 var da = 360 / cnt; //圖片間隔角度 var a0 = 0; //已旋轉角度 var timer; for (var i = 0; i < cnt; i++) { oimages[i].onmouseover = stop; oimages[i].onmouseout = start; } function posimgs() { for (var i = 0; i < cnt; i++) { oimages[i].style.left = centerx + r * Math.cos((da * i + a0) / 180 * Math.PI) + "px"; oimages[i].style.top = centery + r * Math.sin((da * i + a0) / 180 * Math.PI) + "px"; } } // posimgs(); function start() { timer = window.setInterval("posimgs();a0++;", 100); } function stop() { window.clearInterval(timer); } start(); </script> </body> </html>
更多關於JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript查找算法技巧總結》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。