Explore the basics of handling mouse events in JavaScript
see moreJavaScript events
When viewing mouse events, we can interact with it
mousedown
Mouse button pressedmouseup
Release the mouse buttonclick
Click eventdblclick
Double-click eventmousemove
When the mouse moves over the elementmouseover
When the mouse moves over an element or one of its child elementsmouseenter
When the mouse is moved over the element. asmouseover
But it will not bubbling (more on that later!)mouseout
When the mouse moves out of the element, and when the mouse enters the child elementmouseleave
When the mouse moves out of the element. asmouseout
But it will not bubbling (more on that later!)contextmenu
When opening the context menu, for example, when you click on the right mouse button
Events overlap. When you track aclick
Events, like trackingmousedown
With onemouseup
event. in the case ofdblclick
,click
He was also fired twice.
mousedown
,mousemove
withmouseup
Can be used in combination to track drag and drop events.
Be carefulmousemove
, Because it will trigger multiple times during the mouse movement. We need to applyThrottling, This is more of what we will talk about when analyzing scrolling.
In an even number handler, we can access many attributes.
For example, when a mouse event occurs, we can checkbutton
The properties of the event object:
const link = document.getElementById('my-link')
link.addEventListener('mousedown', event => {
// mouse button pressed
console.log(event.button) //0=left, 2=right
})
Here are all the attributes we can use:
altKey
True if the alt key was pressed when the event was triggeredbutton
If so, the number of the button that was pressed when the mouse event was triggered (usually 0 = main button, 1 = middle button, 2 = right button). Act on events caused by clicking a button (for example, click)buttons
If any, a number indicating the button that was pressed on any mouse event.clientX
/clientY
The x and y coordinates of the mouse pointer relative to the browser window, independent of scrollingctrlKey
True if the ctrl key is pressed when the event is triggeredmetaKey
Returns true if the meta key is pressed when the event is triggeredmovementX
/movementY
The x and y coordinates of the mouse pointer relative to the position of the last mousemove event. Used to track the mouse speed when moving the mouseregion
Use in Canvas APIrelatedTarget
The secondary goal of the event, such as when movingscreenX
/screenY
The x and y coordinates of the mouse pointer in screen coordinatesshiftKey
True if the Shift key was pressed when the event was triggered
Download mine for freeJavaScript beginner's manual
More browser tutorials:
- HTML5 provides some useful tips
- How do I make a CMS-based website work offline
- The complete guide to progressive web applications
- Extract API
- Push API guide
- Channel Messaging API
- Service staff tutorial
- Cache API guide
- Notification API guide
- Dive into IndexedDB
- Selectors API: querySelector and querySelectorAll
- Load JavaScript efficiently with delay and asynchrony
- Document Object Model (DOM)
- Web storage API: local storage and session storage
- Understand how HTTP cookies work
- History API
- WebP image format
- XMLHttpRequest (XHR)
- In-depth SVG tutorial
- What is the data URL
- Roadmap for learning network platforms
- CORS, cross-domain resource sharing
- Network worker
- requestAnimationFrame() guide
- What is Doctype
- Use DevTools console and console API
- Speech Synthesis API
- How to wait for DOM ready event in pure JavaScript
- How to add classes to DOM elements
- How to traverse DOM elements from querySelectorAll
- How to remove classes from DOM elements
- How to check if a DOM element has a class
- How to change the DOM node value
- How to add the click event to the list of DOM elements returned from querySelectorAll
- WebRTC, real-time Web API
- How to get the scroll position of an element in JavaScript
- How to replace DOM elements
- How to only accept images in the input file field
- Why use the preview version of the browser?
- Blob object
- File object
- FileReader object
- FileList object
- ArrayBuffer
- ArrayBufferView
- URL object
- Type array
- DataView object
- BroadcastChannel API
- Streams API
- FormData object
- Navigator object
- How to use the geolocation API
- How to use getUserMedia()
- How to use the drag and drop API
- How to scroll on a web page
- Processing the form in JavaScript
- Keyboard events
- Mouse event
- Touch event
- How to remove all children from DOM element
- How to create HTML attributes using raw Javascript
- How to check if the checkbox is checked using JavaScript?
- How to copy to clipboard using JavaScript
- How to disable buttons using JavaScript
- How to make a page editable in the browser
- How to use URLSearchParams to get query string value in JavaScript
- How to delete all CSS on the page at once
- How to use insertAdjacentHTML
- Safari, warning before exit
- How to add an image to the DOM using JavaScript
- How to reset the table
- How to use Google fonts