溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JavaScript如何實現(xiàn)鼠標(biāo)移動事件

發(fā)布時間:2022-08-23 16:34:20 來源:億速云 閱讀:146 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了JavaScript如何實現(xiàn)鼠標(biāo)移動事件的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇JavaScript如何實現(xiàn)鼠標(biāo)移動事件文章都會有所收獲,下面我們一起來看看吧。

實現(xiàn)功能

鼠標(biāo)點擊時可以在畫板上畫畫
如果鼠標(biāo)雙擊那么停止
移動進畫板顏色改變移除時顏色改變

<!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.鼠標(biāo)點擊時可以在畫板上畫畫
        如果鼠標(biāo)雙擊那么停止
        移動進畫板顏色改變移除時顏色改變
        */
      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>

效果圖:

JavaScript如何實現(xiàn)鼠標(biāo)移動事件

關(guān)于“JavaScript如何實現(xiàn)鼠標(biāo)移動事件”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“JavaScript如何實現(xiàn)鼠標(biāo)移動事件”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(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)容。

AI