您好,登錄后才能下訂單哦!
小編給大家分享一下jQuery如何實現小球點擊發射動畫,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
花了兩個小時使用jQuery寫了一個小動畫游戲,如下圖所示,通過鼠標點擊,發射球。
代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> #main { width: 500px; height: 650px; border: 3px solid #efefef; margin: 30px auto; position: relative; } #fireSpace { width: 100%; height: 400px; position: absolute; top: 0; left: 0; } #gun { display: block; width: 60px; height: 60px; position: absolute; bottom: 20px; left: 50%; transform: translate(-50%, 0); } </style> </head> <body> <div id="main"> <div id="fireSpace"> </div> <img src="./gun.png" id="gun"> </div> </body> </html> <script src="./jquery.js"></script> <script> let initX = 0, initY = 300, initDeg = 90, thenDeg, gunX, gunY, boo, x = 0, y = 300, nx, ny, dg = 90, ndg, rdg, isLeft0, isLeft; document.getElementById("fireSpace").onmousemove = function(e) { if (e.offsetX - 220 >= 0) { // nx = e.offsetX - 220; // ny = 600-e.offsetY; gunX = e.offsetX - 220; isLeft = false; } else if (e.offsetX - 220 <= 0) { gunX = 220 - e.offsetX; isLeft = true; } gunY = 650 - e.offsetY; if (e.offsetX - 220 == 0) { thenDeg = 90; } else { thenDeg = gunY - gunX >= 0 ? (90 - Math.asin(gunX / gunY) * 180 / Math.PI) : (Math.asin(gunY / gunX) * 180 / Math.PI); // thenDeg = Math.asin(gunY / gunX) * 180 / Math.PI; } if (initX - 220 == 0) { initDeg = 90; } else { initDeg = initY - initX >= 0 ? (90 - Math.asin(initX / initY) * 180 / Math.PI) : (Math.asin(initY / initX) * 180 / Math.PI); } if (initY <= 3) { initDeg = 0; } if (gunY <= 3) { thenDeg = 0; } if (!isLeft0 && isLeft) { rdg = -(180 - initDeg - thenDeg); } else if (isLeft0 && !isLeft) { rdg = 180 - initDeg - thenDeg; } else if (isLeft0 && isLeft) { rdg = (thenDeg - initDeg) } else if (!isLeft0 && !isLeft) { rdg = (initDeg - thenDeg) } document.getElementById("gun").style.transform = "translate(-50%, 0) rotate(" + rdg + "deg)"; x = nx; y = ny; isLeft0 = isLeft; } let fireX,fireY,iX=0,iY=0 document.getElementById("fireSpace").onclick = function(e) { fireX = e.offsetX; fireY = e.offsetY; let boll = document.createElement("img"); boll.style.width = "50px"; boll.style.height = "50px"; boll.setAttribute("src", "./boll.png"); boll.style.position = "absolute"; boll.style.bottom = "0"; boll.style.left = "50%"; boll.style.transform = "translate(-40%,0)"; boll.style.zIndex = "-1"; document.getElementById("main").appendChild(boll); $(boll).animate({ top: fireY, left: fireX }, 1000); setTimeout(function() { boll.parentNode.removeChild(boll); }, 1000); } </script>
圖片素材:
看完了這篇文章,相信你對“jQuery如何實現小球點擊發射動畫”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。