← Back to jQuery Course | Chapter 3: Events | Lesson 3 of 11

Hover Event

hover pattern तब code चलाता है जब pointer अंदर आता है और बाहर जाता है।
Syntax
javascript
$(selector).hover(
  function () { /* mouse enters */ },
  function () { /* mouse leaves */ }
);

Basic Hover

.hover() दो functions लेता है और उन्हें साथ में जोड़ता है -- पहला तब चलता है जब mouse element में enter करता है, दूसरा जब यह leave करता है। यह essentially mouseenter और mouseleave को अलग-अलग bind करने का shorthand है।

उदाहरण: Basic Hover

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Hover me</div>
    <script>
      $("#box").hover(function() {
      console.log("Mouse entered");
      }, function() {
      console.log("Mouse left");
      });
    </script>
  </body>
</html>

Hover Styling

hover के enter/leave functions में एक style change pass करना, जैसे background-color बदलना, अलग CSS :hover rules लिखे बिना किसी element को visual feedback देने का एक तेज़ तरीका है।

उदाहरण: Hover Styling

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Hover me</div>
    <script>
      $("#box").hover(function() {
      $(this).css("background-color", "yellow");
      }, function() {
      $(this).css("background-color", "");
      });
    </script>
  </body>
</html>

Hover Messages

hover-in पर एक छोटा message या tooltip दिखाना और hover-out पर उसे clear करना lightweight, JavaScript-driven help text के लिए एक common pattern है।

उदाहरण: Hover Messages

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Hover me</div><p id="msg"></p>
    <script>
      $("#box").hover(function() {
      $("#msg").text("You are hovering!");
      }, function() {
      $("#msg").text("");
      });
    </script>
  </body>
</html>

Hover और Classes

enter function में एक class जोड़ना और leave function में उसे हटाना styling logic को आपकी CSS में रखता है जबकि jQuery बस toggle करता है कि कौन-से rules लागू होते हैं, जो आमतौर पर inline style changes से साफ़ है।

उदाहरण: Hover and Classes

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <style>
      .highlighted { background: yellow; }
    </style>
  </head>
  <body>
    <div id="box">Hover me</div>
    <script>
      $("#box").hover(function() {
        $(this).addClass("highlighted");
      }, function() {
        $(this).removeClass("highlighted");
      });
    </script>
  </body>
</html>

Images के साथ Hover

दोनों hover functions के बीच किसी image का src attribute बदलना (या उसका alt text update करना) button states जैसे simple image-based hover effects के लिए एक classic technique है।

उदाहरण: Hover with Images

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <img id="pic" src="button-off.png" alt="Button">
    <script>
      $("#pic").hover(function() {
      $(this).attr("src", "button-on.png");
      }, function() {
      $(this).attr("src", "button-off.png");
      });
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. .hover(fn) को सिर्फ एक function pass करना, इसलिए यह हर एक के लिए अलग handler चलाने के बजाय enter और leave दोनों पर चलता है।
  2. .hover() में .css() से style बदलना और leave function में इसे कभी reset न करना, element को उसकी hover style में stuck छोड़ते हुए।
  3. touch devices के लिए .hover() इस्तेमाल करना और उम्मीद करना कि यह काम करेगा, जबकि touchscreens पर कोई असली hover नहीं होता।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.