Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

HTML DOM MouseEvent

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The MouseEvent Object handles events that occur when the mouse interacts with the HTML document. There are lots of events that interacts with the HTML document.

Mouse Events: Mouse events are the action taken by the user on the web page, like clicking, hovering over, etc.

Mouse EventsDescription
onmouseupOccurs when a user releases a mouse button over an element.
onmousedownOccurs when the button is pressed over an element.
onclickOccurs when the user clicks on an element.
oncontextmenuOccurs on right-clicking on an element
ondblclickOccurs on a double-click on an element.
onmouseenterOccurs when the pointer is moved onto an element.
onmouseleaveOccurs when the pointer is moved out of an element.
onmousemoveOccurs while the pointer moves over an element.
onmouseoutOccurs when a user moves the mouse pointer out of an element, or out of one of its children.
onmouseoverOccurs when the pointer moves over an element or its children.

MouseEvent Properties: These properties are used to inform the defined task if that happens.

Mouse Event PropertiesDescription
altKeyDefine whether the alt key is pressed or not.
buttonDefine the left or right-click events.
clientXReturn the horizontal coordinate of the mouse pointer.
clientYReturn the vertical coordinate of the mouse pointer.
layerXReturn horizontal coordinate relative to the current layer.
layerYReturn vertical coordinates relative to the current layer.
metaKeyIndicates whether or not the “meta” key was pressed.
movementXGive the difference between the X coordinate of the mouse between two events.
movementYGive the difference between the Y coordinate of the mouse between two events.
mozInputSourcGive information about the type of device that generates the event.
offsetXReturning the x-coordinate of the mouse pointer.
offsetYReturning the y-coordinate of the mouse pointer.
pageXReturning the horizontal coordinate of the mouse pointer.
pageYReturning the vertical coordinate of the mouse pointer.
relatedTargetReturning the element that is related to the element that triggered the mouse event.
screenXReturning the horizontal coordinate of the mouse pointer.
screenYReturning the vertical coordinate of the mouse pointer.
shiftKeyDefine whether the shift key is pressed or not.
webkitForceFind pressure applied on the touchpad or touchscreen.
ctrlKeyDefine whether the ctrl key is pressed or not.

 

 

Example: Below program illustrates onmousedown and onmouseup properties.

html




<h1 style="color:green">GeeksforGeeks</h1>
<p id="myP" onmousedown="mouseDown()" onmouseup="mouseUp()">
    The mouseDown() function works when the mouse button
    is pressed down over this paragraph and sets the color
    of the text to 'Yellow'. The mouseUp() function works 
    when the mouse button is released and sets the color
    of the text to 'Red'.
</p>
  
<script>
    function mouseDown() {
      
        // Using onmousedown property
        document.getElementById("myP").style.color = "yellow";
    }
      
    function mouseUp() {
      
        // Using onmouseup property
        document.getElementById("myP").style.color = "red";
    }
</script>

Output:

HTML DOM MouseEvent

HTML DOM MouseEvent

Supported Browsers:

  • Google Chrome 1
  • Mozilla Firefox 1
  • Internet Explorer 9
  • Edge 12
  • Opera 10.6
  • Safari 1

My Personal Notes arrow_drop_up
Last Updated : 02 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials