您好,登錄后才能下訂單哦!
這篇文章主要介紹了js如何實現(xiàn)鼠標點擊飄愛心效果的方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
具體內(nèi)容如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>鼠標點擊特效</title> <link rel="stylesheet" href="https://at.alicdn.com/t/font_1743249_grf4shm1g3t.css" > <style> html { cursor: pointer; } h4 { text-align: center; user-select: none; } .div-box { width: 30px; height: 30px; font-size: 30px; position: absolute; } </style> </head> <body> <h4>請點擊屏幕查看效果</h4> <script> window.onclick = function (e) { //存儲需要的顏色 let arr = ["red", "yellow", "green", "pink", "blue", "purple", "orangered"]; //隨機產(chǎn)生一個顏色 let heartNum = Math.floor(Math.random() * arr.length); let div = document.createElement("div"); div.setAttribute("class", "iconfont iconzan div-box"); div.style.color = arr[heartNum]; document.body.appendChild(div); //獲得鼠標的x,y軸的位置,并減去自身寬高的一半,使其能夠在愛心的正中心 let x = e.pageX - div.offsetWidth / 2; let y = e.pageY - div.offsetHeight / 2; div.style.left = x + "px"; div.style.top = y + "px"; //獲得0-1的整數(shù) let num = Math.round(Math.random()); let timer = setInterval(() => { y -= 10; if (num === 0) x -= 10; else x += 10; div.style.left = x + "px"; div.style.top = y + "px"; //如果超出屏幕范圍,則刪除此節(jié)點 if (y < -100) { clearInterval(timer); div.remove(); } }, 100); } </script> </body> </html>
效果圖如下
感謝你能夠認真閱讀完這篇文章,希望小編分享js如何實現(xiàn)鼠標點擊飄愛心效果的方法內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。