Who doesn’t love motion effects, right? shadows following the motion of movements while in a fighting scene, some of my favorite scenes are from The Matrix, Kung Fu Hustle, etc. I don’t event know how this effect is called, but sure is an awesome effect. The good news is that now-a-days we can create animations and play with them. It’s no an easy task to control distance and motion (the complex the code, the better control). The amazing part is that something like this can be done with 5 lines of jQuery!
Like the article? Be sure to subscribe to our RSS feed and follow us on Twitter to stay up on recent content.
Starting from a simple jQuery animation
If you are cool or at least have worked with jQuery, you can easily implement this animation. I will do an example with a moving ‘dot’ from left to right using animate() function:
$("#ball").stop(true).animate({left:500}, 1000).animate({left:0}, 1000);
The result:
The dot you see above was created using CSS3 (I’m sorry some IE users that I don’t have time for styling this ball, so you are viewing the ugly square. I’m really sorry!):
#ball {
    display: block;
    width: 80px;
    height: 80px;
    background: #333;
    -moz-border-radius: 40px; /* FF1+ */
    -webkit-border-radius: 40px; /* Saf3+, Chrome */
    border-radius: 40px; /* Opera 10.5, IE 9 */
    position: absolute;
    top: 50px;
    left: 50px;
}
Boring? Here is Shadow Motion
Basically, jQuery or even CSS3 doesn’t have any built-in effect or property to make shadows (I mean in plural, more than one shadow). When I watched some fighting scenes in movies, and I wondered, can jQuery can do something like it?

Oh yeah! jQuery can create this effect. The trick is here: lets create some images then make it follow the first animation and fadeout… NOPE, no this way. Lets look back at the example above. jQuery have a function named clone(). The .clone() method is a convenient way to duplicate elements on a page. Use this method, you can duplicate a specific element into many copies. Please note that it doesn’t make sense when we duplicate to many html elements with the same ID attribute. The best way is use class name for the element instead. However, it doesn’t mean we don’t have any way to duplicate the element by ID.
In this case, I will remove it and set a name for class attribute (remember to create css style for new class name):
$("#ball").clone().removeAttr("id").attr({class:"ball"}) .appendTo("#demo");
I’ve tried to duplicated ball in specific numbers. However, the motion depends on the speed of the main element. That’s how we will control the duplicated elements by the time. If the motion is moving fast, we will duplicate it fast, and, if the motion is moving slowly, we’ll need one or two frames duplicated… You need jQuery Timer to do such thing.
In each instance of time, we’ll make one copy.
$("#ball").everyTime(80, function() { $("#ball").clone().removeAttr("id").attr({class:"ball"}) .appendTo("#demo") .everyTime(480, function() { $(this).remove(); }); }); $("#ball").stop(true).animate({left:500}, 1000) .animate({left:0}, 1000, function(){ $("#ball").stopTime(); }); Â
The code above will duplicate one copy every 80 milliseconds. But if don’t break the clone method, you will have have enough balls for Olympic games in 100 years. Inside the everyTime(), I call again this method but restrict it in every 480 ms to remove the duplicated one. Let count, we will have 6 (480/80) copies go after the main element.
The result:
Â
Almost there, Lets do the fadeout effect.
$("#ball").everyTime(80, function() { $(this).animate({left:500}, 1000).animate({left:0}, 1000); $(this).clone().removeAttr("id").attr({class:"ball"}) .appendTo("#demo").fadeOut(480, function() { $(this).remove(); }); });
The final result:
Â
To control the time for duplicating, you can also use window.setInterval(); to replace the everyTime() method.
Boring? See, this is ajax loading effect!! (Windows user please see here)
Â
The code:
function animateit() { var deg=0; var dif=3; var centerX; var centerY; centerX = $('#demo4').width()/4+20; centerY = $('#demo4').height()/2; $("#ball4").everyTime(1, function(){ /* Increment the angle: */ deg+=dif; /* Calculate for the animation in circle */ var eSin = Math.sin(deg*Math.PI/180); var eCos = Math.cos(deg*Math.PI/180); /* Setting the css properties */ $(this).css({ top:centerX+120*eSin, left:centerY+120*eCos }); }); /* Setting up new timer for shadow with 10 shadows in total */ $("#ball4").everyTime(80, function() { $(this).clone().removeAttr("id").attr({"class":"ball"}) .appendTo("#demo4").fadeOut(800, function() { $(this).remove(); }); }); }
Bug?
Yes, the ugly truth about this bug is processor usage. The weird is it happens only with Windows OS even when I’m running a 8Gbs ram machine (Windows 7) – the same with Windows Vista. However, the animation works very well in Mac OS. If you are a Windows user and not happy with what you see here, you can check it via Mac screenshot below.

Any suggestion for a better CPU usage optimization in Windows would be appreciated.
What do you think?
This effect can be used for any animation if you want to implement the shadow motion (I’m not sure if the term I’m using is correct… please help me on getting a proper name). Any suggestion for better control would be appreciated! This tutorial is just providing you an idea for such animation with jQuery. I’ll work on optimize this effect, maybe I will write a little more, then probably publish the plugin. If you want to contribute a suggestion, please do so by commenting, Cheers!


Lam, this is really a very nice effect! I like it a lot and I think I’ll use it for some next project, thanks for sharing.
i agree with you !
Me too
Good work!
.-= Paweł P.´s last blog ..Internet Explorer przełamał kolejną granicę (spada i dobrze) =-.
This is a really creative idea!
Very very cool..what a great effect. I’m thinking James Bond opening scene!
I’ll be giving this a try today thanks for the resource.
I will include more demos in the plugin post. Why don’t you give me feedback when you tried it and would be appreciated with some suggestions!
Very nice
Didn’t know that it is possible in jQuery
Thx for sharing
Works fine on my Windows machine (Vista 2 gigs of ram).
.-= SeanJA´s last blog ..Exceptions Are Not For Flow Control =-.
Hi Sean, how’s about the performance? I saw it so weird in 2 computers running Windows (Vista and 7). The animation is so slowly!
Works fine for me too: Vista, 3GB RAM, Firefox 3.6
Very nice idea. The technique is not difficult but the idea is great!
wow….. great i never know we can make this type of effects ! amazed thanks for the tuts
.-= sriganesh´s last blog ..PSD Tutorial: Design a Dirty Business card =-.
Lam this really great. Everytime I see your jQuery tutorial or Tutzine’s post, just makes me hate flash more. Power to jQuery. Nice post man.
.-= Kawsar Ali´s last blog ..Create a Beautiful Vibrant Watercolor Web Layout =-.
awesome , love u
Nice. Inspired me to try the same with JavaFX: http://karussell.wordpress.com/2010/04/22/reply-via-javafx-on-shadow-motion-effect-in-5-lines-of-jquery/
.-= Peter´s last blog ..Reply via JavaFX on: Shadow Motion Effect in 5 Lines Of jQuery =-.
You are awesome man..
It made me think some other cool effects thanx for the idea.
keep rocking….
.-= Motyar´s last blog ..Dream Night Animation with Jquery =-.
Hey, you know what, your path plugin is in my list of best plugins! Thanks for that!
wow, excellent idea. Simple and effect, thanks!
.-= Matthew Heidenreich´s last blog ..40 Stunning Nature-Inspired Web Designs =-.
This is brilliant…
Hello my friend
How to add the ” Share with Digg,Facebook,Stum,..” bellow your title post ?
Thanks
.-= Äạt Tà i´s last blog ..Tạo Slider giá»›i thiệu bà i viết sá» dụng jQuery =-.
loved it. got tip for my next web application
So easy to do! So much you can do with this. Thanks! Also, it works fine on XP using Boot Camp.
Really, Really, you rocks… Amazing totu
Keep it up Lam 
.-= SmashingWebs.com´s last blog ..JillzTheme : New Magazine WordPress PSD Theme =-.
WOW Very very good work, it fantastic shadow.
Superb plugin! Thanks.
nice plugin
wow…i like it…hihihihi any shadow
Cool plugin with sweeping implications.
very nice plugin thanks lot
.-= esranull´s last blog ..create an origami logo =-.
Very clever man
Thanks, Soh! Just from movies, I think!
Brilliant Lam! Some of your tutorials just blow me away. Great work, as usual!
.-= Shawn Ramsey´s last blog ..A Handful Of CSS Trends And How To Use Them =-.
Works fine in Chrome, on Win XP SP3 with 3GB Ram – but yes, in IE8.0 it is a square…
Yah, because IE doesn’t support rounded corner at all!
Great tutorial
.-= Ivan´s last blog ..WordPress Comments Notifier =-.
Very clever and nice effect… jQuery is a great tool for smart people like you… dear Lam.
Cheers!
.-= piervix´s last blog ..Touch The Future: Create An Elegant Website With HMTL 5 And CSS3 =-.
Absolutely unusable on Firefox @Linux
# cat /proc/version
Linux version 2.6.33-ARCH (tobias@T-POWA-LX) (gcc version 4.4.3 20100316 (prerelease) (GCC) ) #1 SMP PREEMPT Sun Apr 4 10:27:30 CEST 2010
# cat /proc/cpuinfo vendor_id : GenuineIntel
cpu family : 6
model : 15
model name : Intel(R) Core(TM)2 Duo CPU L7500 @ 1.60GHz stepping : 11
cpu MHz : 800.000
cache size : 4096 KB
# cat /proc/meminfo
MemTotal: 3982172 kB
Nice tip. Thanks Lam!
.-= Web Design Soul´s last blog ..5 Misconceptions About Freelance Design =-.
first applikation ever seen that runs better on mac …
Interesting effect! However… quite greedy with the RAM and CPU
Yah, it looks weird in Windows. It needs optimization really!
APESTA
Sorry to say so, but this pretty much doesnt work at all in Chromium on Ubuntu Lucid , 2 GB RAM
Yah, this is its bug. I’m trying to optimize it!
Nice plugin. Needs work though, Kills CPU after a while on FF 3.6 on Mac OS X. But overall – Nice!
.-= Jake Noble´s last blog ..Nibbler Alpha Launched =-.
Clever, very clever !!!
This was a great post! I love the effect!
very good post.
It’s wonderfull.
awesomely creative. This once again proves that great things can be achieved with simple tools.
Great tut!
I’m using Windows 7 with a 4 RAM machine and it works perfectly.
its wonderful. Thank you
perfect, çok iyi iş çıkartmışlar.
awesome jquery effects, i think these of all an replacement of flash animations.
Hey. I’ve got a quad-core 27-inch iMac with 8GB RAM, and after about 45 seconds of the animation, my system hangs. I think it’s because you are continuously adding/removing DOM elements with this method. I think you’d be better off adding all of the balls at once, then toggling their opacity.
Thanks for sharing the technique.
Nice idea! Thanks for sharing, I really appreciate your comment!
This is so cool! I like it a lot and its a great, easy-to-follow tutorial. Thanks for sharing.
Wow thanks!
its great . thanks