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

animate() से Custom Animations

animate समय के साथ numeric CSS properties बदलता है।
Syntax
javascript
$(selector).animate({ property: value }, duration, easing, callback);

Basic animate()

animate() method किसी element पर एक या ज़्यादा numeric CSS properties को एक set duration में smoothly नई target values तक transition करती है। यह सिर्फ numeric values वाली properties पर काम करती है, जैसे width या opacity, बिना किसी plugin के colors पर नहीं।

उदाहरण: Basic animate()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box" style="width:100px">Box</div>
    <script>
      $("#box").animate({width: "300px"});
    </script>
  </body>
</html>

कई Properties

कई properties वाला एक object pass करना, जैसे {width: 200px, opacity: 0.5}, उन सभी को एक call में साथ में animate करता है, आपको कई अलग .animate() calls chain करने से बचाते हुए जो वरना out of sync चलतीं।

उदाहरण: Multiple Properties

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box" style="width:100px;opacity:1">Box</div>
    <script>
      $("#box").animate({width: "200px", opacity: 0.5});
    </script>
  </body>
</html>

Duration

animate() का दूसरा argument set करता है कि transition में कितना समय लगता है, या तो milliseconds के रूप में या slow या fast जैसा keyword, आपको animation pacing पर direct control देते हुए।

उदाहरण: Duration

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box" style="width:100px">Box</div>
    <script>
      $("#box").animate({width: "300px"}, 2000);
    </script>
  </body>
</html>

Opacity

Opacity उन कुछ non-pixel numeric properties में से एक है जिन्हें animate() out of the box समझता है, आपको एक बड़े multi-property animation के हिस्से के रूप में किसी element को fade in या out करने देते हुए।

उदाहरण: Opacity

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Box</div>
    <script>
      $("#box").animate({opacity: 0});
    </script>
  </body>
</html>

Relative Values

किसी target value को += या -= से prefix करना, जैसे {left: '+=50px'}, किसी property को उसकी absolute value के बजाय उसकी current value के relative animate करता है, जो किसी element को उसकी starting position चाहे जो भी हो एक fixed amount से nudge करने के लिए उपयोगी है।

उदाहरण: Relative Values

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box" style="position:relative;left:0px">Box</div>
    <script>
      $("#box").animate({left: "+=50px"});
    </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. non-numeric values animate करने की कोशिश करना, जैसे .animate({color: "red"}), जो jQuery core बिना किसी plugin के नहीं कर सकता।
  2. hyphens वाले CSS names लिखना जैसे {background-color: "red"}, जो एक syntax error है; object में camelCase backgroundColor इस्तेमाल करें।
  3. बिना set size या display:inline वाले किसी element पर width animate करना, इसलिए कुछ होता नहीं दिखता क्योंकि inline elements width को नज़रअंदाज़ करते हैं।

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.