A while ago I wrote a little “pulse” plugin for jQuery which would make an element pulse between two or more states. I won’t bother linking to it because it’s offline. I decided to have another go at developing it, this time with a more refined and intuitive API.
I’ve tried to emulate the characteristics of jQuery’s animate()
method as much as possible. There are only a few slight differences. Here’s a typical call to the new “pulse” plugin:
jQuery(element).pulse({ opacity: [0, 1], backgroundColor: ['red', 'yellow'] }, 1000, 5, 'linear', function(){ alert("I'm done pulsing!"); }); |
It’s almost exactly the same parameter pattern as animate()
, except that you have to specify an array of values for each CSS property, and also an additional numerical parameter, after the duration, which signifies how many times you want the pulse to run (the above code will run the pulse five times).
It also works with an “options” object, just like animate()
:
jQuery(element).pulse({ opacity: [0,1] }, { duration: 100, // duration of EACH individual animation times: 3, // Will go three times through the pulse array [0,1] easing: 'linear', // easing function for each individual animation complete: function() { alert("I'm done pulsing!"); } }); |
You should note that the duration
option only defines how long each individual animation will run, not the entire pulse. To dictate the length of the entire pulse you need to use the times
option. Also note that the times
option refers to an entire run-through of the largest array found in the properties option.
You can “pulse” through as many values as you want:
jQuery(element).pulse({ backgroundColor: ['red', 'yellow', 'green', 'blue'], opacity: [0, 1], }); |
The arrays don’t all have to be of equal length – with the above code, the opacity would keep changing as is defined by its array.
The plugin itself is quite small. I’ve decided to start a new repo at Github, just for small and useful jQuery plugins like this.
I haven’t tested it extensively yet, so, as usual please notify me of any bugs/issues. Thanks!
Thanks for reading! Please share your thoughts with me on Twitter. Have a great day!
A demo page would be nice (or did I miss it?).
How about an online demo for those too lazy to clone your GitHub repo? 😉 I quickly took
demo.html
and threw it in JS Bin: http://jsbin.com/iqujoThanks @Mathias 🙂
Is it just me or is the backgroundColor pulsing option not working?
The opacity pulse works just fine.
@Colin, you need to either be using jQuery UI, or download this, for color animations to work (
backgroundColor
,borderColor
, etc.)How would you set the pulse, but have it turn off when you hover – here’s my code that doesn’t work:
Help please!! 🙂
@Mike,
Brilliant!! 😉
Gracias James!
Neat little plugin! Used it instead of jQuery UI’s pulsate plugin because I didn’t wanted to include the whole bunch of of required libraries as UI core, UI Effects core…
I know how this plugin can refine
This is perfect!!! just what I was hoping to find. Brilliant work.
Thanks James! I’ve been using the old version for a while and hoping for an update. It’s perfect for what I need.
I’m having a problem where a pulsating div does not return to full opacity after I stop it. Even when I try to manually set it’s opacity it seems not to work. Can somebody tell me what I’m doing wrong? Here is the code snippet:
status_div.parent().parent().stop();
status_div.parent().parent().fadeTo(‘fast’, 1);
The first line successfully stops the div from pulsating, but the second line does not return it to full opacity.
Thanks,
Carl
@Carl, works for me: http://jsbin.com/uzole3
Hi James, I’m running into an error in IE7, and I was wondering if you might be able to help me. I’m pulsing a background and border color like this:
I’m using the Color Animations script and I’ve added the ‘borderColor’ color style. It runs great in Firefox and Chrome works pretty well (it doesn’t pulse the border, which is fine), but I get an “Invalid property value” error occassionally in IE7. The error is in the Color Animations script, here’s the line:
The error happens on char 13 of the first line, if that helps. Under the debugger locals, fx.end has a value of “#ff0000” and fx.start has a value of 0. Thanks for any help!
Ok, I did a little more searching and found a question on stackoverflow. The solution that worked for me was to change a line in the Color animations script from this:
to this:
I haven’t tested it extensively yet, but so far so good.
Once again, hi James,
i tried to create 2 instances of pulse plugin and found that its not working for 2 divs.
below is the code i used
$(‘#demo’).pulse({
opacity: [0,1]
}, {
times: 999999
});
$(‘#demo1’).pulse({
opacity: [0,9]
}, {
times: 999999
});
is there any solution ??
Thanks for providing this! Is it possible to tell the element to pulse continuously (times: infinite) until it’s told to stop? What I’m trying to do is have an element start pulsing when hovered and keep pulsing until mouseout.
@amit, I think you want to change
[0,9]
to[0,1]
.@John, I’ll probably be adding this feature in the future but for now just use a big number that’ll probably never be reached (e.g.
99999
).cool, nice, best, thanks for the share 🙂
Handy little plugin, thanks!
Please also register my vote for continuous/infinite pulsing, perhaps using the “times” value “-1”
And it would be nice to be able to specify different easings for in and out rather than just the same one for both. Or is that already possible somehow?
I’m linking to the Jquery color doc (http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js) and trying to use the backgroundColor feature:
$(“#myDiv”).pulse({
opacity: [0,1]
}, {
duration: 100,
backgroundColor: [‘red’],
times: 3,
easing: ‘linear’
});
And no go…Does this still work with Jquery 1.4.2?
Can anyone confirm this works with JQuery 1.4.2? I am having trouble getting it to work after loading 1.4.2 (worked fine in 1.3.1). I am using the Google API version…
Thanks!
@Chris, works for me… http://jsfiddle.net/9sWRT/
@Toomany, what background colors do you want to pulse to/from? You need more than one. E.g. http://jsfiddle.net/7sQ5c/
Also, @Toomany, you should specify all CSS properties in the first object (with “opacity”). The second object (with “times”, “duration” etc.) is just for extra options.
Hi James,
How do you restart the Pulse. I stop the pulse to do some processing, but if a condition isn’t met, I would like to restart the pulse. From the comments, I’m using $(this).stop().fadeTo(‘fast’, 1); to stop things while I process.
Regards,
Terry
Awesome plugin. This is exactly what I have been looking for on my latest web project.
It’s especially handy – as Alex states in an earlier comment – that it saves the need of including the UI library, etc.
Much appreciation. I will be including a link in an upcoming blog post (when I manage to get my blog live).
Kudos!