05.24.2009

jQuery Animate

Bookmark and Share
Add to DZone

Here I will mention how to use the jquery animate function and the usability and flexibility of it.
The syntax of this function is:

$.animate(options, settings, callback);
/* you can replace setting with duration (in milliseconds) or words: 'slow', 'normal' or 'fast'.
callback is optional */

Below is a list of popular and most used animation:

  • increasing/decreasing width and height
  • moving or scrolling html element
  • changing opacity
  • scrolling window

Now let me explain each in an example.

Follow up:



increasing/decreasing width and height

Increasing div width with animation has a simple code:
having a div with 100% width and height of 250px

$('div').animate({'width':'10%','height':'50px'},1500); /* This will decrease the width to 10% and height to 50 pixels with animation. */

As you can see, we may use one property or more than one property separated with commas.
see it in action:

Click here to animate width and height.



moving or scrolling html element

To change html element's position with animate, you can change the css top and left properties as follows:

$('div').animate({top:'100px', left:'100px'},1500);

see it in action:

Click here to change this div's location.



changing opacity

Changing opacity of html element can be animated as follows:

$('div').animate({opacity:0.1},1000); /* Changes the opacity from 1 to 0.1 while 1 is the normal view. */

see it in action:

Click here to change this div's opacity.



scrolling window

This item was added to this blog before in Scroll window smoothly in jQuery

$('html, body').animate({
scrollTop: $("#scrollToHere").offset().top
}, 2000);
}


You can do more with this function. Try more properties.
See also:
jQuery Animate - Advanced

13 comments

Comment from: Fred [Visitor]
$('div').find('*').andSelf().css({'backgroundColor':'#fff'}).end().animate({opacity:0.1},1000);

Always make sure you have a background color for all children nodes containing text node, or you will have serious issues with XP Media Center Edition, XP Tablet PC
and Vista for Internet Explorer 6 and 7 when ClearType fonts is enabled.

On Vista, if you have ClearType fonts, then what will happen is that piece of text will be non-ClearType, while the other will be.

So if you have a bunch of ClearType text nearby
and one animated over opacity that one will look weird.
On MCE/TabletPC, the font becomes bold and bad kerning, simply just horrible.

Instead of * you can specify span or p

The problem arise as this:

[div][span]asdf[/span][/div]

What is the backgroundColor for span ?

It's transparent.

How do I do opacity on text over transparent on whatever div background in Internet Explorer with ClearType...

IE has no clue and the font rendering is horrible,
with ClearType off everything works fine,
with XP SP2 + ClearType + IE6 == no problem,
XP SP2 + ClearType + IE6 + MediaCenterEdition package == problem.

If you have something like this:

[div id='container' style='background:blue;width:200px;float:left;']
[div id='animateMe' style='background:white;margin:5px 5px;width:180px;float:left;'][div style='text-align:center;']
[span]asdf[/span][/div][/div][/div]

$('#animateMe').animate({opacity:0.8},1000);

You'll run into problems too.
span is transparent.

Worst, the only way to get that right in my case was to wrap the container into a table and force a white background on that table, tbody and td for IE to behave correctly...
to really get the whitish effect I had with Firefox
and normal XP2/IE6

You can look on Google for this "opacity cleartype"
http://jszen.blogspot.com/2005/04/ie-bold-text-opacity-problem.html

BTW, even alpha=100 does not fix it, as soon as alpha is there you get the problem.

05/28/09 @ 03:57
Comment from: scotchegg [Visitor] · http://www.scotche.gg
Hey nice write up.

Ive been playing with animate, but I am not sure if this is an issue with the animate function or the way i am coding it.

I have a div i animate to widen to a new width and it slides nicely. However the containing div with float:right does not move to the new right side edge of the animated div, so it leaves a gap. Any ideas?

$('.gotoPortfolio').click(function() {

$('.barslide').animate({width:750},1500);

return false;

});
06/28/09 @ 15:36
Comment from: Freelancer ID [Member] Email · http://www.freelancer-id.com
I didn't code anything with floating DIV inside. Well, i need to test this and I will provide you with the reply shortly.

Thanks for the comment
06/28/09 @ 17:57
Comment from: Freelancer ID [Member] Email · http://www.freelancer-id.com
I have tested the issue you provided about right floating and animation... The result was that everything is working fine and the floating div stays the the right side while animating.
In this case, while you are experiencing a problem, you may have a CSS conflict.
Please give a chance to see the code to provide you with the solution.
06/29/09 @ 10:12
Comment from: ugg boots [Visitor] · http://uggbootshop.net
the entry is good!
01/21/10 @ 09:36
Very positive blog! I'm really impressed )
04/02/10 @ 00:26
Comment from: dk [Visitor]
$("id").animate({"height": "toggle"}, { duration: 1000 }); is not properly works in IE 8. why?
04/15/10 @ 09:41
Comment from: Freelancer ID [Member] Email · http://www.freelancer-id.com
Try to use:

$("#id").slideToggle(speed);

This will toggle the height from 0 to actual height OR reverse.
04/15/10 @ 17:54
Comment from: Howard [Visitor] · http://www.rapidskunk.com
I visit regularly and always something interesting like this is posted. Thanks again.
04/22/10 @ 19:24
Comment from: builder [Visitor] Email · http://www.buildvoy.com/
Very nice ;)
04/25/10 @ 20:11
Comment from: Tutorials99 [Visitor] · http://www.tutorials99.com
Thanks for the this, it’s extremely extensive and useful!,do check out the link below

http://www.tutorials99.com

where all tutorials have a Higher page rank and professional.Very helpful for beginners…
05/23/10 @ 22:03
Comment from: Alqin [Visitor] Email
What about if...
1. i have a div display:none,
2. i want to use the efect of slideDown() to show it,
3. but starting from a height of 100px (dynamic value)
4. to a height set by the content within?
06/24/10 @ 19:58
Comment from: Jake [Visitor] · http://www.mmpersonalloans.com
This is my first time i visit here. I found so many interesting stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the excellent work.
06/28/10 @ 00:23

This post has 1 feedback awaiting moderation...

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
Free Blog Themes / Templates