Rounded corners images with jQuery

Use

Apply CSS class “rounded” to img tag

<img class="rounded" src="..." />

How it works

Four white GIFs are positionned at the corners.

CSS

.rounded_wrapper {
	position: relative;
}
 
.rounded_wrapper img {
	border-width: 0;
	border-style: none;
}
 
.rounded_wrapper div {
	height: 7px;
	position: absolute;
	width: 100%;
}
 
.rounded_wrapper .tl {
	top: 0;
	left: 0;
	background: url(img/rounded_corners/tl.gif) no-repeat left top;
}
 
.rounded_wrapper .tr {
	top: 0;
	right: 0;
	background: url(img/rounded_corners/tr.gif) no-repeat right top;	
}
 
.rounded_wrapper .br {
	bottom: 0;
	right: 0;
	background: url(img/rounded_corners/br.gif) no-repeat right bottom;	
}
 
.rounded_wrapper .bl {
	bottom: 0;
	left: 0;
	background: url(img/rounded_corners/bl.gif) no-repeat left bottom;
}
 
/* IE6 fix */
.ie6_width .tr {
    right: -1px;
}
 
.ie6_width .br {
    right: -1px;
}
 
.ie6_height .br {
    bottom: -1px;
}
 
.ie6_height .bl {
    bottom: -1px;
}

Javascript

$('img.rounded').one('load',function () {
	var img = $(this);
	var img_width = img.width();
	var img_height = img.height();
 
	// build wrapper
	var wrapper = $('<div class="rounded_wrapper"></div>');
	wrapper.width(img_width);
	wrapper.height(img_height);
 
	// move CSS properties from img to wrapper
	wrapper.css('float', img.css('float'));
	img.css('float', 'none')
 
	wrapper.css('margin-right', img.css('margin-right'));
	img.css('margin-right', '0')
 
	wrapper.css('margin-left', img.css('margin-left'));
	img.css('margin-left', '0')
 
	wrapper.css('margin-bottom', img.css('margin-bottom'));
	img.css('margin-bottom', '0')
 
	wrapper.css('margin-top', img.css('margin-top'));
	img.css('margin-top', '0')
 
	wrapper.css('display', 'block');
	img.css('display', 'block')
 
	// IE6 fix (when image height or width is odd)
	if ($.browser.msie && $.browser.version == '6.0')
	{
		if(img_width % 2 != 0)
		{
			wrapper.addClass('ie6_width')
		}
		if(img_height % 2 != 0)
		{
			wrapper.addClass('ie6_height')			
		}
	}
 
	// wrap image
	img.wrap(wrapper);
 
	// add rounded corners
	img.after('<div class="tl"></div>');
	img.after('<div class="tr"></div>');
	img.after('<div class="bl"></div>');
	img.after('<div class="br"></div>');
}).each(function(){
	if(this.complete) $(this).trigger("load");
});

Notes

Reference

 

Feedback

I'm a little unsure why you are use the jQuery.load function (which is an ajax http request). I think .each is what you are looking for.
Drew
April 16, 2009
#1
Jérôme Jaglale
April 16, 2009
#2
This is great! This is perfect for using within a CMS and giving the client options to round their images. Thanks for putting this up....
Rhianon
June 21, 2009
#3
Hmmm..maybe I take that back.... In several browsers, if you go to another page, then come back to where your rounded corners were they are no longer rounded. If you ever come up with a fix for this, let me know.. parhelion77 AT gmail com
Rhianon
June 21, 2009
#4
Drew, you are right. You should use each instead of load. Works in FF IE6 IE7 IE8
Johan
January 29, 2010
#5
And thank you for a VERY usefull plugin
Johan
January 29, 2010
#6
Update:

- fix for IE 6 when image has odd width or/and height
- fix for problem mentioned by Rhianon above
Jérôme Jaglale
July 9, 2010
#7