I have already written a post on my blog on how to create jQuery drop shadow effect. You can check it out here. The plugin basically creates a div behind those elements to be shadowed to create drop shadow effect. See the demo of that old version here.
I have now modified the plugin and it now uses CSS3 for drop shadow effect for the browsers that support it or the same div-based shadow for non-supporting browsers. Here is the code:
/**
* Drop Shadow Plugin jQuery
* http://sarfraznawaz.wordpress.com/
* Author: Sarfraz Ahmed ([email protected])
*/
(function($){
$.fn.dropshadow = function(settings){
// Extend default settings
var opts = $.extend({}, $.fn.dropshadow.defaults, settings);
// Check if CSS3 is supported
var style = $('div')[0].style;
var isCSS3 = style.MozBoxShadow !== undefined || style.WebkitBoxShadow !== undefined || style.BoxShadow !== undefined;
return this.each(function(settings){
var options = $.extend({}, opts, $(this).data());
var $this = $(this);
if (!isCSS3){
var styles = {
position: 'absolute',
width: $this.width() + 'px',
height: $this.height() + 'px',
backgroundColor: options.shadowColor,
zIndex: options.shadowLayer,
top: ($this.offset().top + parseInt(options.distance, 10)) + 'px',
left: ($this.offset().left + parseInt(options.distance, 10)) + 'px'
};
}
else{
var boxshadow = options.distance + ' ' + options.distance + ' ' + options.blur + ' ' + options.shadowColor;
var styles = {
position: 'absolute',
width: $this.width() + 'px',
height: $this.height() + 'px',
backgroundColor: options.shadowColor,
zIndex: options.shadowLayer,
top: $this.offset().top + 'px',
left: $this.offset().left + 'px',
MozBoxShadow:boxshadow,
WebkitBoxShadow:boxshadow,
BoxShadow:boxshadow
};
}
$('<div>').appendTo($('body')).css(styles);
});
}
// set default option values
$.fn.dropshadow.defaults = {
shadowColor: '#DFDFDF',
shadowLayer: -1,
distance:'5px',
blur:'3px'
}
})(jQuery);
Here is how to use it:
$(window).load(function(){
$('.shadow').dropshadow({
shadowColor: '#cccccc',
shadowLayer: -100,
distance:'6px',
blur:'3px'
});
});