Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery .mouseleave() Method

Posted in jQuery Tutorial
Updated on Oct 13, 2024
By Mari Selvan
👁️ 61 - Views
⏳ 4 mins
💬 1 Comment
jQuery .mouseleave() Method

Photo Credit to CodeToFun

🙋 Introduction

jQuery offers a wide range of methods to facilitate dynamic web development, and one such method is .mouseleave(). This method allows you to perform actions when the mouse pointer leaves an element. Understanding and leveraging the .mouseleave() method can add interactivity and enhance user experience on your website.

In this guide, we'll explore the jQuery .mouseleave() method with practical examples to demonstrate its usage and benefits.

🧠 Understanding .mouseleave() Method

The .mouseleave() method in jQuery is used to bind an event handler function to the "mouseleave" event, which occurs when the mouse pointer leaves the selected element. This event is particularly useful for triggering actions such as hiding tooltips, resetting animations, or changing styles when the user moves the mouse away from a specific area on the webpage.

💡 Syntax

The syntax for the .mouseleave() method is straightforward:

syntax.js
Copied
Copy To Clipboard
$(selector).mouseleave(function() {
  // Code to be executed when mouse leaves the element
});

📝 Example

  1. Hiding Tooltip on Mouse Leave:

    Suppose you have a tooltip that appears when hovering over an element and you want it to disappear when the mouse moves away. You can achieve this using the .mouseleave() method as follows:

    index.html
    Copied
    Copy To Clipboard
    <div class="tooltip">
      Hover over me
      <span class="tooltiptext">Tooltip text</span>
    </div>
    index.css
    Copied
    Copy To Clipboard
    .tooltip {
      position: relative;
      display: inline-block;
    }
    
    .tooltiptext {
      visibility: hidden;
      position: absolute;
      /* Tooltip styles */
    }
    
    .tooltip:hover .tooltiptext {
      visibility: visible;
    }
    example.js
    Copied
    Copy To Clipboard
    $(".tooltip").mouseleave(function() {
      $(this).find(".tooltiptext").css("visibility", "hidden");
    });

    This will hide the tooltip when the mouse pointer leaves the .tooltip element.

  2. Resetting Animation on Mouse Leave:

    You can also use the .mouseleave() method to reset CSS animations when the mouse leaves a specific element. For instance:

    index.html
    Copied
    Copy To Clipboard
    <div class="box"></div>
    example.js
    Copied
    Copy To Clipboard
    .box {
      width: 100px;
      height: 100px;
      background-color: blue;
      transition: width 0.5s, height 0.5s;
    }
    
    .box:hover {
      width: 200px;
      height: 200px;
    }
    example.js
    Copied
    Copy To Clipboard
    $(".box").mouseleave(function() {
      $(this).css({
        width: "100px",
        height: "100px"
      });
    });

    This will reset the size of the .box element when the mouse pointer leaves it.

🎉 Conclusion

The jQuery .mouseleave() method provides a convenient way to trigger actions when the mouse pointer exits an element, allowing you to create interactive and user-friendly web experiences. Whether you need to hide tooltips, reset animations, or perform other tasks upon mouse exit, this method simplifies the process.

By mastering its usage, you can enhance the interactivity and usability of your website effectively.

👨‍💻 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