← Back to jQuery Course | Chapter 4: Effects & Animations | Lesson 2 of 9

Fading Effects

fadeIn धीरे-धीरे किसी hidden element को visible बनाता है।
Syntax
javascript
$(selector).fadeIn(speed, callback);
$(selector).fadeOut(speed, callback);
$(selector).fadeToggle(speed, callback);
$(selector).fadeTo(speed, opacity, callback);

fadeIn()

fadeIn() किसी hidden element की opacity को एक चुनी गई duration में 0 से 1 तक animate करके इसे reveal करता है। यह सिर्फ transparency को असर करता है, इसलिए element का size और position पूरी animation के दौरान fixed रहते हैं।

उदाहरण: fadeIn()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box" style="display:none">Fading in</div>
    <script>
      $("#box").fadeIn(1000);
    </script>
  </body>
</html>

fadeOut()

fadeOut() इसका उल्टा animation चलाता है, किसी element की opacity को धीरे-धीरे 0 तक कम करता है और फिर fade पूरा होने पर display:none set करता है, इसे layout से हटाते हुए। क्योंकि element को बाद में display:none set किया जाता है, यह सिर्फ invisible नहीं बल्कि page के layout flow से पूरी तरह हटा दिया जाता है।

उदाहरण: fadeOut()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Fading out</div>
    <script>
      $("#box").fadeOut(1000);
    </script>
  </body>
</html>

fadeToggle()

fadeToggle() check करता है कि कोई element इस समय visible है या faded out, और इसे opposite state में animate कर देता है, आपको एक single call देते हुए जो दोनों directions के लिए काम करती है। यह एक सुविधाजनक shortcut है जब आप किसी method को call करने का फ़ैसला लेने से पहले किसी element की current visibility state खुद track नहीं करना चाहते।

उदाहरण: fadeToggle()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Toggle fade</div>
    <button id="btn">Fade</button>
    <script>
      $("#btn").on("click", function() {
      $("#box").fadeToggle();
      });
    </script>
  </body>
</html>

fadeTo()

fadeTo() किसी element को आपकी चुनी हुई एक specific opacity value पर animate करता है, जैसे 0.5, हमेशा पूरी तरह 0 या 1 तक जाने के बजाय -- एक dimmed या semi-transparent effect के लिए उपयोगी। यह simple fade in या out से ज़्यादा fine control देता है, dimmed overlay या एक subtle hover effect बनाने के लिए उपयोगी।

उदाहरण: fadeTo()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Dimmed content</div>
    <script>
      $("#box").fadeTo(1000, 0.5);
    </script>
  </body>
</html>

Fade Speed

हर fading method एक duration argument accept करता है -- या तो milliseconds या slow या fast जैसा keyword -- control करते हुए कि opacity transition पूरा होने में कितना समय लगता है। आप एक second argument के रूप में एक callback function भी pass कर सकते हैं, जो fade animation खत्म होते ही automatically चलता है।

उदाहरण: Fade Speed

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Content</div>
    <script>
      $("#box").fadeOut("slow", function() {
      console.log("Fade complete");
      });
    </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. CSS में visibility:hidden या opacity:0 वाले किसी element पर .fadeIn() call करना और कुछ नहीं देखना, क्योंकि यह सिर्फ display:none को reverse करता है।
  2. milliseconds के बजाय seconds pass करना, जैसे .fadeIn(1), जो सिर्फ 1 ms तक चलता है; एक second के लिए .fadeIn(1000) इस्तेमाल करें।
  3. .fadeTo(400, 0) को .fadeOut() के साथ confuse करना, जबकि .fadeTo element को opacity 0 पर layout में छोड़ देता है और .fadeOut() display:none के साथ खत्म होता है।

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.