animate() से Custom Animations
In this page:
$(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()
<!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
<!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
<!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
<!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
<!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>
- non-numeric values animate करने की कोशिश करना, जैसे
.animate({color: "red"}), जो jQuery core बिना किसी plugin के नहीं कर सकता। - hyphens वाले CSS names लिखना जैसे
{background-color: "red"}, जो एक syntax error है; object में camelCasebackgroundColorइस्तेमाल करें। - बिना set size या
display:inlineवाले किसी element परwidthanimate करना, इसलिए कुछ होता नहीं दिखता क्योंकि inline elements width को नज़रअंदाज़ करते हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: