SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • Azure (0)
  • LINQ (5)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box
Thursday, August 26, 2010 : 7:14 AM - (0 comments)

Generic "Animate" function with jQuery

OK, so I needed the ability to animate the "max-height" css property for a project I'm doing at work. We extensively use jQuery in the project, and I know that jQuery has the animate function - so I thought this was going to be a quick answer. I couldn't figure it out, so I figured I'd ask the experts "In jQuery, how do I animate the “max-height” CSS property?"

As it turns out, the animate function expects only a set list of properties that can be animated (such as opacity, height, etc). Max-height is not one of the properties you can animate!

Basically what I needed was the ability to "animate" anything. To be more specific, I needed to be able to specify a 'from' and 'to' value, and have my own callback function executed with the progress along the way. It was an easy method to implement, but I figured I would post it here so that others who may need the same functionality could have it as well.

The jQuery 'animate' function is very awesome, and this function is in no way to replace that.

Quick Example of a Custom jQuery Function

So here goes. First off, to extend jQuery to have your own custom method is fairly simple. Just to illustrate how easy it is to do it, I'll show you a method I came up with a while back, the "outerHTML" method:

// Include this script somewhere after including your jQuery.js file.
(function($) {

   // Extend the jQuery object to have an 'outerHTML' method.

   $.fn.outerHTML = function() {
       // Pretty simple code, clone the object, stick it in a div

       // and then grab the 'html' of that div (which is therefore

       // the 'outerHTML' of the object in question.

       return $("<div>").append(this.clone()).html();
   };

})(jQuery);

Now, with that method you can do something like this:

alert( $("#some-div").outerHTML() );

Generic Animate jQuery Extension

Now that you understand how to create your own custom jQuery extension, you'll be able to follow along with this code a bit easier. There's not a lot of code, so I'll just paste the entire method and include an explanation in JavaScript comments.

// By the way, this line here just let's me use the '$' character to represent the
// 'jQuery' object. If you don't get it, don't worry about it - it's not important.

(function ($) {

   // Create our new 'animateX' function (stupid name, I know)

   $.fn.animateX = function animate(from, to, duration, callback) {

   // A little sanity check code... if there is no real 'distance' to animate,

   // then we might as well just break out right now.

   if (((to - from) / duration) == 0) {
       return;
   }

   // Capture the time we started.

   var start = new Date();

   // This is kinda interesting, we want to run our code on all of the objects

   // that the supplied 'jQuery' matches. Example $("div#abc") would only find

   // one object... but $("span") would match all spans and execute this code on

   // all of them. NOTE: in the method below, 'i' is the index (which we don't care

   // about) and 'j' is the current object (current "span" in my example above).

   this.each(function(i, j) {

       // Make a temp function that does the actual 'animating', we need to

       // do this because we are going to keep calling this method in a loop

       // using the setTimeout method.

       var cont = function (s, f, t, d, c) {
           // Calculate the current "position"... that is, milliseconds

           // elapsed multiplied by the distance of a "step" (to minus

           // from divided by duration).

           var p = ((new Date()) - start) * ((t - f) / d);

           // Did we progress at all? If so, call the callback!

           if (p != 0) {
               // Value = "from" + "position"

               var v = f + p;
               // Ensure that we haven't gone past the "to" (our end

               // point). If we are going forward, make sure we haven't

               // gone 'greater than to', and if we are animating in the

               // opposite direction (ex: from 10 to 5), then make sure

               // we haven't gone too far behind.

               v = (p > 0)
               ? ((v < t) ? v : t)
               : ((v > t) ? v : t);

               // Now that we have a value, call our callback passing in

               // the value and the current object.

               callback(v, j);
           }

           // Finally, if we haven't yet reached our destination, re-run this

           // code in "1 millisecond". NOTE: Your computer won't actually run

           // this code for about 15 to 30 milliseconds in most cases.

           if (v != t) setTimeout(function () { cont(s, f, t, d, c); }, 1);
       }

       // Oh yeah, kick off the process one time right now to begin with.

       cont(start, from, to, duration, callback);
   });
};

})(jQuery);

Here's a demo if you'd like to see an example of what this would do: generic animate with jQuery demo.html

Enjoy!

Developer / Architect / Author

Blog Archives

  • August 2010 - (1)
  • June 2009 - (1)
  • January 2009 - (1)
  • November 2008 - (1)
  • October 2008 - (2)
  • September 2008 - (2)
  • August 2008 - (3)
  • July 2008 - (1)
  • June 2008 - (3)
  • May 2008 - (2)
  • April 2008 - (2)
  • March 2008 - (4)
  • February 2008 - (2)
  • December 2007 - (2)
  • November 2007 - (1)
  • October 2007 - (4)
  • September 2007 - (9)
  • August 2007 - (7)

Related Ads

SingingEels.com as of Feb 04 2012 - 12:37:25 AM - (0.0625012)