jQuery Blink Plugin
As part of a website that I am building I wanted to be able to draw the users attention to a content that is already on the page. This type of behavior may have purposes else where, so I encapsulated the content into a jQuery plugin. The following screen cast shows the desired effect.
The jQuery Plugin
The following is the JavaScript that defines the jQuery plugin:
(function($) { $.fn.blink = function(options) { var defaults = { highlightClass: "highlight", blinkCount: 3, fadeDownSpeed: "slow", fadeUpSpeed: "fast", fadeToOpacity: 0.33 }; var options = $.extend(defaults, options); return this.each(function() { var obj = $(this); var blinkCount = 0; obj.addClass(options.highlightClass); doBlink(); function doBlink() { if (blinkCount < options.blinkCount) { obj.fadeTo(options.fadeDownSpeed, options.fadeToOpacity, function() { obj.fadeTo(options.fadeUpSpeed, 1.0, doBlink); }); } else { obj.removeClass(options.highlightClass); } blinkCount++; } }); }; })(jQuery);
The plugin exposes a few options:
- highlighClass – This is the CSS class that is added to the element just before the first blink and is removed after the last blink.
- blinkCount – This is the number of blinks that occur.
- fadeDownSpeed, fadeUpSpeed – These options set the fade speeds.
- fadeToOpacity – This sets the lower opacity used in the blink routine.
The blink effect is realized by cycling the opacity of the element between ‘fadeToOpacity’ and 1. Additional highlighting is achieved using a CSS class.
How do I use this?
In my contact form example I have a link that is defined using the following HTML:
<p>Contact us via <a id="other-contact" href="#contact-info">snail mail or phone</a>.</p>
I also have a contact area defined by the following HTML:
<div id="contact-info"> <h5>Contact Info</h5> <p>Bob Cravens</p> <p>123 South Main Street</p> <p>Verona, Wisconsin 53593</p> <p>1.234.567.8888</p> </div>
Notice the href attribute uses a link to a named element. This will cause the browser to scroll to the element if it happens to be off the page. To use the jQuery plugin we hook up the click event as follows:
$(document).ready(function() { $("#other-contact").click(function() { $("#contact-info").blink({ blinkCount: 5 }); }); });
The above JavaScript uses the jQuery ready event to wire up the click event for the contact anchor. Inside the click event we use jQuery to select the elements that we want to make blink and then call the ‘blink’ function. Notice in this example, I have an over ride for the ‘blinkCount’ option.
Summary
Hope this is useful. Let me know if you have improvements. The jQuery library continues to amaze me.
Really nice post,thank you
Hi,
Can you provide some simple working demo page so that I can check how exactly you have used the code? I can get the link to move the page to footer but I can not get it to blink.
Also if this would smoothly scroll down the page, it would be great!
Here is a quick example. It is mostly copy and paste from the blog article to jsfiddle.
http://jsfiddle.net/rcravens/YxYMB/
Bob
Hi!
nice work. I just add the ininity option :
var defaults = {
highlightClass: “highlight”,
blinkCount: 3,
fadeDownSpeed: “slow”,
fadeUpSpeed: “fast”,
fadeToOpacity: 0.33,
infinity: 0,
};
and instead of :
blinkCount++;
I wrote :
if(options.infinity==0) { blinkCount++; }
I call it like that :
$(“.high”).blink({
fadeDownSpeed: “slow”,
fadeUpSpeed: “slow”,
infinity: 1,
});
Enjoy !
I have modified your plugin. Now you can start and stop blinking.
(function($) {
$.fn.blink = function(options) {
var defaults = {
highlightClass: ‘highlight’,
blinkCount: 3,
fadeDownSpeed: ‘fast’,
fadeUpSpeed: ‘fast’,
fadeToOpacity: 0.33,
infinity: 0,
};
var options = $.extend(defaults, options);
return this.each(function() {
var obj = $(this);
obj.data(‘blinkCount’,options.blinkCount);
obj.addClass(options.highlightClass);
doBlink();
function doBlink() {
if (obj.data(‘blinkCount’)>0) {
obj.fadeTo(options.fadeDownSpeed, options.fadeToOpacity, function() {
obj.fadeTo(options.fadeUpSpeed, 1.0, doBlink);
});
} else {
obj.removeClass(options.highlightClass);
}
if(options.infinity==0) {
obj.data(‘blinkCount’,obj.data(‘blinkCount’)-1)
}
}
});
};
})(jQuery);
function blink_on(id){
$(id).blink({
blinkCount: 5,
infinity: 1
});
}
function blink_off(id){
$(id).blink({
blinkCount: 0
});
}