Earlier we saw examples of jQuery click and jQuery double click events. They are categorised as jQuery mouse events. Today we will look into six more jQuery mouse events that you can use for various animations and effects.

  1. jQuery mousedown():The mousedown() method fires when you press the mouse on any HTML element. This method will attach an event handler to the mouse down event. We can do any operations by using this event handler.The syntax for using mousedown():In this, mousedown() method is used without an argument.

    The handler could be any function which is executed each time the event is fired

  2. jQuery mouseup(): The mouseup() method fires when the mouse is released.This method will attach an event handler to the mouse up event. We can do any operations by using this event handler.The syntax for using mouseup():In this, mouseup() method is used without an argument.

    The handler could be any function which is executed each time the event is fired.

  3. jQuery mouseenter(): The mouseenter() method fires when the mouse enters an HTML element. This method will attach an event handler to the mouse enter event. We can do any operations by using this event handler.The syntax for using mouseenter():In this, mouseenter() method is used without an argument.

    The handler could be any function which is executed each time the event is fired

  4. jQuery mouseleave(): The mouseleave() method fires when the mouse leaves the selected element. This method will attach an event handler to the mouse leave event.The syntax for using mouseleave():In this, mouseleave() method is used without an argument.

    The handler could be any function which is executed each time the event is fired

  5. jQuery mouseover(): The mouseover() method fires when the mouse enters a selected HTML element. The main difference between the mouseover() and mouseenter() method is; mouseover() method is fired even if the mouse enters the child element of the selected element.The syntax for using mouseover():In this, mouseover() method is used without an argument.

    The handler could be any function which is executed each time the event is fired

  6. jQuery mousemove(): The mousemove() method fires when the mouse is moved within the selected HTML element. This method will attach an event handler to the mouse move event.The syntax for using mousemove():In this, mousemove() method is used without an argument.

    The handler could be any function which is executed each time the event is fired

jQuery mouseup and mousedown example

Below is a simple example showing usage of jQuery mouse up and mouse down event methods.

In this example, you can see that mousedown() method is triggered when you press on the <div> element and a text message “mouse Down” is displayed. When you release the mouse, mouseup() method is fired and a text message “mouse UP” is displayed.

The main difference between the click() method and these two methods is that click() method triggers only after the mouse is pressed and released. If you want to do anything in between these two actions, we can use mousedown() and mouseup().

Below is the output of above HTML page, try it yourself by clicking and holding for sometime before releasing the mouse.

jQuery mouseover, mouseenter, mouseleave and mousemove example

Below example shows the use of remaining mouse events.

From the above example, you can easily learn the use and difference between these methods.

In this example, you can see that mouseover() method is triggered when you enter the green region, which is the parent <div> element and the yellow region the child <p> element. Unlike the mouseover() method, mouseenter() method fires when the mouse enters the green region. The mouseleave() method triggers when you leave the selected html element and mousemove() event triggers whenever the mouse is moved within the selected element.

Below is the output of above HTML example, you can try it yourself.

These are the main jQuery mouse event methods, there are few more that we will discuss in coming posts.

By admin

Leave a Reply

%d bloggers like this: