Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery Basic

jQuery Ajax Events

jQuery Ajax Methods

jQuery Keyboard Events

jQuery Keyboard Methods

jQuery Form Events

jQuery Form Methods

jQuery Mouse Event

jQuery Mouse Methods

jQuery Event Object

jQuery Fading

jQuery Document Loading

jQuery Traversing

jQuery Utilities

jQuery Property

jQuery HTML

jQuery CSS

jQuery Miscellaneous

jQuery mousemove Event

Posted in jQuery Tutorial
Updated on Nov 01, 2024
By Mari Selvan
👁️ 29 - Views
⏳ 4 mins
💬 1 Comment
jQuery mousemove Event

Photo Credit to CodeToFun

🙋 Introduction

jQuery offers a plethora of event handling functions to make web development more interactive and responsive. One such event is mousemove, which is triggered when the mouse pointer is moved over an element. Although the .mousemove() method has been deprecated, you can achieve the same functionality using the .on() method.

In this guide, we'll explore the jQuery mousemove event, its syntax using .on(), and provide examples to demonstrate its practical usage.

🧠 Understanding mousemove Event

The mousemove event occurs when the mouse pointer is moved over an element. It's commonly used to track the movement of the mouse and trigger actions accordingly. With jQuery's event handling capabilities, you can easily attach functions to this event and create dynamic interactions on your web page.

💡 Syntax

The syntax for the mousemove event is straightforward:

syntax.js
Copied
Copy To Clipboard
$(selector).on("mousemove", handler);

or with optional event data:

syntax.js
Copied
Copy To Clipboard
$(selector).on("mousemove", eventData, handler);

📝 Example

  1. Tracking Mouse Movement:

    Suppose you want to track the mouse movement within a specific element and display the coordinates. You can achieve this by binding a mousemove event handler to that element:

    index.html
    Copied
    Copy To Clipboard
    <div id="tracker" style="width: 300px; height: 200px; background-color: lightgray;"></div>
    example.js
    Copied
    Copy To Clipboard
    $("#tracker").on("mousemove", function(event) {
      $("#coordinates").text("X: " + event.pageX + ", Y: " + event.pageY);
    });

    This will display the current coordinates of the mouse pointer within the #tracker element.

  2. Creating Interactive Effects:

    You can use the mousemove event to create interactive effects, such as changing the background color of an element based on the mouse position:

    index.html
    Copied
    Copy To Clipboard
    <div id="interactive" style="width: 300px; height: 200px; background-color: lightblue;"></div>
    example.js
    Copied
    Copy To Clipboard
    $("#interactive").on("mousemove", function(event) {
      var x = event.pageX / $(this).width();
      var y = event.pageY / $(this).height();
      $(this).css("background-color", "rgb(" + (x * 255) + "," + (y * 255) + ", 100)");
    });

    This will change the background color of the #interactive element based on the mouse position, creating a dynamic color effect.

  3. Managing Event Data:

    You can pass additional data to the event handler using the .on() method. This can be useful for customizing behavior based on specific conditions or contexts.

🎉 Conclusion

The jQuery mousemove event, although deprecated in its method form, remains a fundamental tool for creating interactive and dynamic web experiences. By leveraging the .on() method, you can easily attach event handlers to elements and respond to mouse movement with precision.

Whether you're tracking coordinates, creating interactive effects, or implementing custom behaviors, mastering the mousemove event opens up a world of possibilities for enhancing user interaction on your web pages.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy