Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
Animate() in jQuery is a pre-defined function that uses the CSS properties for performing sequential or non-sequential animation customization with the help of the queue & toggle functionality. The syntax for this function is ‘$(selector).animate(properties, options)’, which contains many CSS properties and parameters. The commonly used parameters are duration, easing and function similar to other jQuery functions, and also other parameters like queue, complete, start, done, fail and step.
$(selector).animate(properties, options);
Below are the details of the Parameters:
Examples of Animate in jQuery
Below given are some examples of animate in jQuery:
This is an example of the animate() method. we are animating the height and width of the box.
Code:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>Example of animate in jquery</title>
<script src=”https://code.jquery.com/jquery-1.12.4.min.js”></script>
<script>
$(document).ready(function(){
$(“#btn”).click(function(){
$(“#square”).animate({height: “500px”}, “slow”);
$(“#square”).animate({width: “500px”}, “slow”);
});
});
</script>
</head>
<body>
<button id=”btn”>Animate height and width</button>
<div id=”square” style=”background:blue;height:100px;width:100px;margin:6px;”></div>
</body>
</html>
Output:
Animating the top, bottom, right and left by using the animate() method. In this, we need to provide the position. The position takes 3 values. They are absolute, fixed and relative we can assign the position values according to our requirement.
Code:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>Example of animate in jquery</title>
<script src=”https://code.jquery.com/jquery-1.12.4.min.js”></script>
<script>
$(document).ready(function(){
$(“#btn”).click(function(){
$(“#box1”).animate({bottom: ‘150px’});
$(“#box2”).animate({top: ‘150px’});
$(“#box3”).animate({right: ‘150px’});
$(“#box4”).animate({left: ‘150px’});
});
});
</script>
</head>
<body>
<button id=”btn”>Animate top, bottom, right and left</button>
<div id=”box1″ style=”background:blue;height:100px;width:100px;margin:6px;position:relative;”></div>
<div id=”box2″ style=”background:blue;height:100px;width:100px;margin:6px;position:relative;”></div>
<div id=”box3″ style=”background:blue;height:100px;width:100px;margin:6px;position:relative;”></div>
<div id=”box4″ style=”background:blue;height:100px;width:100px;margin:6px;position:relative;”></div>
</body>
</html>
Output:
The toggle effect by using the animate() method.
Code:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>Example of animate in jquery</title>
<script src=”https://code.jquery.com/jquery-1.12.4.min.js”></script>
<script>
$(document).ready(function(){
$(“#btn”).click(function(){
$(“#square”).animate({width: ‘toggle’});
});
});
</script>
</head>
<body>
<button id=”btn”>Animate width</button>
<div id=”square” style=”background:blue;height:100px;width:100px;margin:6px;”></div>
</body>
</html>
Before clicking on the button animate width.
Change in the font size by using the animate() method. In this example initially, it will take the default font size. In the animate method, we can even increase the font size so we have increased the font size to 5em.
Code:
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>Example of animate in jquery</title>
<script src=”https://code.jquery.com/jquery-1.12.4.min.js”></script>
<script>
$(document).ready(function(){
$(“#btn”).click(function(){
$(“#square”).animate({fontSize: ‘3em’});
});
});
</script>
</head>
<body>
<button id=”btn”>Animate fontsize</button>
<div id=”square” style=”background:blue;height:300px;width:300px;margin:6px;”>Change in fontsize by using animate method</div>
</body>
</html>
Output:
How to queue parameter is used in the animate method.
Code:
<html lang=”en”>
<body>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js”></script>
<script>
</script>
<style type=”text/css”>
div {
width: 150px;
height: 150px;
background-color: blue;
}
</style>
</head>
<body>
<div></div>
<br/>
<button id=”animate”>Click here</button>
<script type=”text/javascript”>
$(“#animate”).click(function() {
$(“div”).animate({
width: “250px”,
height: “250px”,
borderRadius: “50%”,
marginLeft: “110px”,
marginTop: “50px”,
},
1000,
);
$(“div”).animate({width:’150px’},1000).animate({borderLeftWidth:’150px’},1000);
});
</script>
</body>
</html>
learn more about programming at mcp
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.