Sample code, instructions and cases for JavaScript to handle various mouse events

When using JavaScript to handle mouse events, you can use HTML code to explain the usage, function, and common use cases of each event.

  1. click (click event): Triggered when the mouse clicks on an element.
<button id="myButton">点击我</button>

<script>
  var button = document.getElementById('myButton');
  
  button.addEventListener('click', function(event) {
      
      
    // 处理点击事件
    alert('按钮被点击了');
  });
</script>

Function: When the button is clicked, a prompt box pops up showing "The button was clicked".

Common use cases: Create a button in a web page, and when the user clicks the button, the corresponding action is triggered, such as submitting a form, opening a pop-up window, etc.

  1. mouseover (mouse-in event): Fired when the mouse moves over an element.
<div id="myDiv">将鼠标移入此处</div>

<script>
  var div = document.getElementById('myDiv');
  
  div.addEventListener('mouseover', function(event) {
      
      
    // 处理鼠标移入事件
    div.style.backgroundColor = 'red';
  });
</script>

Function: When the mouse moves into <div>the element, the background color of the element is changed to red.

Common use case: On a navigation menu in a web page, change the style of the menu item to provide visual feedback when the user moves the mouse over the menu item.

  1. mouseout (mouse out event): Triggered when the mouse moves out from above the element.
<div id="myDiv">将鼠标移出此处</div>

<script>
  var div = document.getElementById('myDiv');
  
  div.addEventListener('mouseout', function(event) {
      
      
    // 处理鼠标移出事件
    div.style.backgroundColor = 'white';
  });
</script>

Function: When the mouse <div>is moved away from the element, the background color of the element is restored to white.

Common use cases: On images in web pages, when the user moves the mouse out of the image area, restore the original style of the image or display other relevant information.

  1. mousemove (mouse movement event): Fired when the mouse moves within the element.
<div id="myDiv">在此处移动鼠标</div>

<script>
  var div = document.getElementById('myDiv');
  
  div.addEventListener('mousemove', function(event) {
      
      
    // 处理鼠标移动事件
    console.log('鼠标位置:', event.clientX, event.clientY);
  });
</script>

Function: When the mouse <div>moves within the element, the coordinate position of the mouse is output on the console.

Common use cases: tracking the position of the mouse movement to implement drawing or elements in the canvas or drag function in the web page

Drag effect.

  1. mousedown (mouse down event): Triggered when the mouse presses a button.
<button id="myButton">按下我</button>

<script>
  var button = document.getElementById('myButton');
  
  button.addEventListener('mousedown', function(event) {
      
      
    // 处理鼠标按下事件
    button.innerHTML = '按钮已按下';
  });
</script>

Function: When the mouse presses the button, the button text is modified to "Button Pressed".

Common use cases: In games or interactive interfaces on web pages, when the user presses a specific button, the corresponding game operation or interface switch is triggered.

  1. mouseup (mouse release event): Triggered when the mouse releases a button.
<button id="myButton">松开我</button>

<script>
  var button = document.getElementById('myButton');
  
  button.addEventListener('mouseup', function(event) {
      
      
    // 处理鼠标松开事件
    button.innerHTML = '按钮已松开';
  });
</script>

Function: When the mouse releases the button, the button text is modified to "Button has been released".

Common use cases: When using the drag-and-drop function on a web page or pressing and holding a button for continuous operations, the corresponding logic processing is triggered by monitoring the mouse release event.

The above sample code demonstrates how to use JavaScript to handle common mouse events in HTML, and gives the usage, role and common use cases of each event. You can write corresponding logic in the event handling function to respond to the user's mouse operations based on specific needs and scenarios.

Guess you like

Origin blog.csdn.net/qq_43326668/article/details/130863298
Recommended