The jQuery animate() method is used to create custom animations. The animate() method is typically used to animate numeric CSS properties, for example, width, height, margin, padding, opacity, top, left, etc. but the non-numeric properties such as color or background-color cannot be animated using the basic jQuery functionality.

Syntax

The basic syntax of the jQuery animate() method can be given with:

[tabby title=”jQuery”]

$(selector).animate({ properties }, duration, callback);

[tabbyending]

The parameters of the animate() method have the following meanings:

  • The required properties parameter defines the CSS properties to be animated.
  • The optional duration parameter specifies how long the animation will run. Durations can be specified either using one of the predefined string ‘slow’ or ‘fast’, or in a number of milliseconds; higher values indicate slower animations.
  • The optional callback parameter is a function to call once the animation is complete.

Here’s a simple example of the jQuery animate() method that animates an image from its original position to the right by 300 pixels on click of the button.

[tabby title=”Example”]

<script type=”text/javascript”>
$(document).ready(function(){
$(“button”).click(function(){
$(“img”).animate({
left: 300
});
});
});
</script>

[tabbyending]

Animate Multiple Properties At Once

You can also animate multiple properties of an element together at the same time using the animate() method. All the properties animated simultaneously without any delay.

[tabby title=”Example”]

<script type=”text/javascript”>
$(document).ready(function(){
$(“button”).click(function(){
$(“.box”).animate({
width: “300px”,
height: “300px”,
marginLeft: “150px”,
borderWidth: “10px”,
opacity: 0.5
});
});
});
</script>

[tabbyending]

Animate Multiple Properties One by One or Queued Animations

You can also animate the multiple properties of an element one by one individually in a queue using the jQuery’s chaining feature. We’ll learn more about chaining in next chapter.

The following example demonstrates a jQuery queued or chained animation, where each animation will start once the previous animation on the element has completed.

[tabby title=”Example”]

<script type=”text/javascript”>
$(document).ready(function(){
$(“button”).click(function(){
$(“.box”)
.animate({width: “300px”})
.animate({height: “300px”})
.animate({marginLeft: “150px”})
.animate({borderWidth: “10px”})
.animate({opacity: 0.5});
});
});
</script>

[tabbyending]

Animate Properties with Relative Values

You can also define the relative values for the animated properties. If a value is specified with a leading += or -= prefix, then the target value is calculated by adding or subtracting the given number from the current value of the property.

[tabby title=”Example”]

<script type=”text/javascript”>
$(document).ready(function(){
$(“button”).click(function(){
$(“.box”).animate({
top: “+=50px”,
left: “+=50px”,
width: “+=50px”,
height: “+=50px”
});
});
});
</script>

[tabbyending]

Animate Properties with Pre-defined Values

In addition to the numeric values, each property can take the strings 'show''hide', and 'toggle'. It will be very helpful in a situation when you simply want to animate the property from its current value to the initial value and vice versa.

[tabby title=”Example”]

<script type=”text/javascript”>
$(document).ready(function(){
$(“button”).click(function(){
$(“.box”).animate({
width: ‘toggle’
});
});
});
</script>

[tabbyending]

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Request a Quote

Let our code do the miracles for your website

We are an award winning web agency with over 10 years of experience in web development. In the past, we have helped numerous businesses establish their online presence through clean and semantic code. Our dedicated team of developers ensure the success of your project by paying attention to detail and by going that extra mile with you until the results are achieved.

</ THEHTMLCODER.COM >

Copyright 2018 - THEHTMLCODER.COM