JavaScript實現鼠標移動事件畫筆
JavaScript實現鼠標移動事件畫筆小遊戲,供大傢參考,具體內容如下
實現功能
鼠標點擊時可以在畫板上畫畫
如果鼠標雙擊那麼停止
移動進畫板顏色改變移除時顏色改變
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> </head> <style> * { margin: 0; padding: 0; box-sizing: border-box; } .drawbox { width: 1100px; height: 600px; background-color: skyblue; position: relative; } </style> <body> <div class="drawbox"></div> <script> /* 1.鼠標點擊時可以在畫板上畫畫 如果鼠標雙擊那麼停止 移動進畫板顏色改變移除時顏色改變 */ var darwbox = document.querySelector("div"); darwbox.onmouseenter = function() { darwbox.style.backgroundColor = "red"; }; darwbox.onmouseleave = function() { darwbox.style.backgroundColor = "skyblue"; }; var istrue = false; darwbox.onmousedown = function(e) { istrue = true; }; darwbox.onmousemove = function(e) { if (istrue == true) { var x = e.pageX; var y = e.pageY; var circle = document.createElement("div"); circle.style.width = "10px"; circle.style.height = "10px"; circle.style.backgroundColor = "#fff"; circle.style.position = "absolute"; circle.style.left = x - 5 + "px"; circle.style.top = y - 5 + "px"; circle.style.borderRadius = "50%"; darwbox.appendChild(circle); } }; darwbox.onmouseup = function(e) { istrue = false; }; </script> </body> </html>
效果圖:
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。