77

Which is the correct syntax to animate the box-shadow property with jQuery?

$().animate({?:"0 0 5px #666"});
neophyte
  • 6,540
  • 2
  • 28
  • 43
chchrist
  • 18,854
  • 11
  • 48
  • 82

5 Answers5

80

Direct answer

Using Edwin Martin's jQuery plugin for shadow animation, which extends the .animate method, you can simply use the normal syntax with "boxShadow" and every facet of that - color, the x- and y-offset, the blur-radius and spread-radius - gets animated. It includes multiple shadow support.

$(element).animate({ 
    boxShadow: "0px 0px 5px 3px hsla(100, 70%, 60%, 0.8)"
}) 

Using CSS animations instead

jQuery animates by changing the style property of DOM elements, which can cause surprises with specificity and moves style information out of your stylesheets.

I can't find browser support stats for CSS shadow animation, but you might consider using JS to apply an animation-based class instead of handling the animation directly. For example, you can define a box-shadow animation in your stylesheet:

@keyframes shadowPulse {
    0% {
        box-shadow: 0px 0px 10px 0px hsla(0, 0%, 0%, 1);
    }

    100% {
        box-shadow: 0px 0px 5px 0px hsla(0, 0%, 0%, 0);
    }
}
    
.shadow-pulse {
    animation-name: shadowPulse;
    animation-duration: 1.5s;
    animation-iteration-count: 1;
    animation-timing-function: linear;
}

You can then use the native animationend event to synchronise the end of the animation with what you were doing in your JS code:

Vanilla JS:

element.classList.add('shadow-pulse')
element.addEventListener('animationend', event => {  
    element.classList.remove('shadow-pulse')
    // do something else...
})

jQuery:

$(element).addClass('shadow-pulse')
$(element).on('animationend', function(){    
    $(element).removeClass('shadow-pulse')
    // do something else...
})
iono
  • 2,575
  • 1
  • 28
  • 36
  • 1
    I agree, the accepted answer hasn't been update recently. This plugin worked nicely for me. – barro32 Jun 27 '12 at 01:39
  • I can't seem to get this to work without errors using jQuery 1.9 anyone else encountered this? – Skami Jan 22 '13 at 12:17
  • Given that it only came out, what, a week ago(?), he probably hasn't updated it yet. Have a read through the [jQ1.9 upgrade guide](http://jquery.com/upgrade-guide/1.9/) and the [shadow animation plugin's source](http://www.bitstorm.org/jquery/shadow-animation/archive/jquery.animate-shadow.js) and see what methods need to be reworked, if it's urgent. – iono Jan 23 '13 at 03:16
  • I used the archive you linked instead of the latest version of his plugin and it worked flawlessly. So that solved my problem thanks for the quick reply. – Skami Jan 23 '13 at 08:24
  • No worries, but - wow, that was completely accidental. I must have clicked on the wrong link. Maybe try the unminified 1.8 version; see if it was a minification error? Seems strange that the 1.7 version would work better than 1.8 with 1.9. – iono Jan 23 '13 at 08:38
  • 1
    I've tried both unminified and the minified version yesterday and both gave me the same error. I'm just happy it works now I just hope I won't run into any of the problems that solved in 1.8. I also hope that Edwin will continue to develop this plugin as it seems to be the version with the least hassle to use out there. – Skami Jan 23 '13 at 09:04
  • Using with jQuery 1.8.3 and it works fine but be sure you set default box shadow (for example: none) in your CSS... Spent some time to figure it out. – webrama.pl Dec 16 '14 at 09:16
  • @Si8 here's not the place for bug reports - contact Edwin Martin about that. Also "it doesn't work" is not a very informative bug report – iono Mar 15 '16 at 05:40
  • Oh yeah sorry... I meant it doesn't animate the shadow and just displays the text. I will reach out to him. Thanks. – Si8 Mar 15 '16 at 11:15
  • I am new to jQuery and JavaScript in general. I used I Edwin Martin's plugin and tried adding the CDN tag to the html template I have, and then in my `app.js`, I tried something like `$("#elementId").animate({boxShadow: ...});` and that didn't work. Am I missing something? – KareemJ Sep 25 '20 at 16:28
30

If you are using jQuery 1.4.3+ then you can take advantage of the cssHooks code that was added.

By using the boxShadow hook: https://github.com/brandonaaron/jquery-cssHooks/blob/master/boxshadow.js

You can do something like this:

$('#box').animate({
    'boxShadowX': '10px',
    'boxShadowY':'10px',
    'boxShadowBlur': '20px'
});

The hook doesn't let you animate the color yet but I am sure with some work that could be added.

Example: http://jsfiddle.net/petersendidit/w5aAn/

PetersenDidIt
  • 25,562
  • 3
  • 67
  • 72
  • Are cssHooks part of 1.4.3 or a plugin? I'd love a link to some jQuery documentation if you've got one, this sounds interesting. – Surreal Dreams Nov 09 '10 at 13:48
  • @Surreal Part of 1.4.3. cssHooks haven't been documented much yet but you can dive in to the sorce and check it out: https://github.com/jquery/jquery/blob/master/src/css.js Brandon Arron has been doing a lot of playing with cssHooks: https://github.com/brandonaaron/jquery-cssHooks/ – PetersenDidIt Nov 09 '10 at 20:29
  • @Blowsie probably is a problem with the boxshadow cssHook. Maybe same as this problem: https://github.com/brandonaaron/jquery-cssHooks/issues/32 – PetersenDidIt Jun 29 '11 at 15:38
  • This doesn't seem to support inset box-shadows, or am I mistaken? – Adam Fraser Jan 22 '12 at 04:17
  • @AdamFraser works for me (tested in Chrome) http://jsfiddle.net/petersendidit/w5aAn/474/ – PetersenDidIt Jan 22 '12 at 05:05
  • Ah, that works. So you just need to specify `inset` beforehand. Got it. – Adam Fraser Jan 22 '12 at 16:03
  • FYI: @tomeoftom's answer is more modern and works exceptionally well. – Jordan Arsenault Jul 05 '12 at 00:09
15

If you don't want to use a plugin, it can be done with a bit of CSS:

#my-div{    
  background-color: gray;
  animation: shadowThrob 0.9s infinite;
  animation-direction: alternate;
  -webkit-animation: shadowThrob 0.9s ease-out infinite;
  -webkit-animation-direction: alternate;
}
@keyframes shadowThrob {
  from {box-shadow: 0 0 30px 10px rgba(190,65,12, 0.9);}
  to {box-shadow: 0 0 30px 10px rgba(190,65,12, 0.2);}
}
@-webkit-keyframes shadowThrob {
  from {box-shadow: 0 0 30px 10px rgba(190,65,12, 0.9);}
  to {box-shadow: 0 0 30px 10px rgba(190,65,12, 0.2);}
}
/*NOTE: The animation property is not supported in Internet Explorer 9 and earlier versions.*/

Check it out: http://jsfiddle.net/Z8E5U/

If you want full documentation on CSS animations, your path to wizardry begins here..

ShaneSauce
  • 209
  • 2
  • 9
  • Nice one, indeed. Somehow, this one doesn't burn most of my processor running the animation. I think, but I'm not sure though, that it was due to the fact that I used 2 ranges of **percentage** for the keyframes (0%, 50% and 100%). – jozi Apr 26 '15 at 21:59
4

I love the ShaneSauce solution ! Use a class intead of an ID and you can add/remove the effect to any element using jQuery addClass/delay/removeClass :

$('.error').each( function(idx, el){
    $( el )
        .addClass( 'highlight' )
        .delay( 2000 )
        .removeClass( 'highlight' );
});
Glaubule
  • 169
  • 1
  • 7
0

Here is an example of how to do it without a plugin: jQuery animated Box But it doesn't have the extra functionality that comes with the use of a plugin, but I personally like to see (and understand) the method behind the madness.

Lahmizzar
  • 487
  • 6
  • 6
Drazion
  • 342
  • 1
  • 4
  • 12