jQuery Tip: Font Resizing With Animation Effects

Friday, January 16th, 2009

Hey everyone. Sorry for not having a new tip published yesterday, I was feeling under the weather. Seeing how popular the original font resizing with jQuery article was, I thought it would be nice to spice up our original script with a little animation. This is extremely easy to do with jQuerys built in animation functions.

The Original Script

$(function(){
		$('input').click(function(){
			var ourText = $('p');
			var currFontSize = ourText.css('fontSize');
			var finalNum = parseFloat(currFontSize, 10);
			var stringEnding = currFontSize.slice(-2);
			if(this.id == 'large') {
				finalNum *= 1.2;
			}
			else if (this.id == 'small'){
				finalNum /=1.2;
			}
			ourText.css('fontSize', finalNum + stringEnding);
		});
	});

Achieving the Animation Effect

Instead of instantly resizing the text upon click, we want the text to re size over a specified amount of milliseconds. We can still use our original script but we will replace the last section from the css() method to the animate() method.

Our New Script

Here is our new script complete with animation effects.

$(function(){
		$('input').click(function(){
			var ourText = $('p');
			var currFontSize = ourText.css('fontSize');
			var finalNum = parseFloat(currFontSize, 10);
			var stringEnding = currFontSize.slice(-2);
			if(this.id == 'large') {
				finalNum *= 1.2;
			}
			else if (this.id == 'small'){
				finalNum /=1.2;
			}
			ourText.animate({fontSize: finalNum + stringEnding},600);
		});
	});

Quite simple isn't it? Get creative and add your own touch or animations to your jQuery effects.

View the Demo


If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

Hi, I'm Drew Douglass and I make sure Dev-Tips.com runs smoothly. I also work for Envato Support and write for NETTUTS.com and ThemeForest.net. I'm passionate about anything web development related, especially php, WordPress, MySQL, and jQuery. Feel free to follow me on twitter.
  1. January 20, 2009 at 3:31 pm
    • January 20, 2009 at 3:37 pm
  2. January 21, 2009 at 5:12 pm
    • January 21, 2009 at 10:58 pm
      • January 25, 2009 at 3:40 pm
  3. Tom
    January 23, 2009 at 9:51 am
    • January 23, 2009 at 1:45 pm
  4. alex
    February 3, 2009 at 7:33 am
  5. April 30, 2009 at 12:27 am
  6. May 3, 2009 at 4:37 am
  7. May 10, 2009 at 9:48 am
  8. May 19, 2009 at 10:21 am
  9. May 19, 2009 at 10:45 am
  10. June 1, 2009 at 11:31 pm
  11. July 12, 2009 at 5:13 pm
    • July 12, 2009 at 6:02 pm
  12. October 1, 2009 at 2:22 am
  13. October 29, 2009 at 12:33 am
  14. January 25, 2009 at 10:58 pm

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates