2 * jQuery corner plugin: simple corner rounding
3 * Examples and documentation at: http://jquery.malsup.com/corner/
4 * version 2.06 (16-FEB-2010)
5 * Requires jQuery v1.3.2 or later
6 * Dual licensed under the MIT and GPL licenses:
7 * http://www.opensource.org/licenses/mit-license.php
8 * http://www.gnu.org/licenses/gpl.html
9 * Authors: Dave Methvin and Mike Alsup
13 * corner() takes a single string argument: $('#myDiv').corner("effect corners width")
15 * effect: name of the effect to apply, such as round, bevel, notch, bite, etc (default is round).
16 * corners: one or more of: top, bottom, tr, tl, br, or bl. (default is all corners)
17 * width: width of the effect; in the case of rounded corners this is the radius.
18 * specify this value using the px suffix such as 10px (yes, it must be pixels).
22 var style = document.createElement('div').style;
23 var moz = style['MozBorderRadius'] !== undefined;
24 var webkit = style['WebkitBorderRadius'] !== undefined;
25 var radius = style['BorderRadius'] !== undefined;
26 var mode = document.documentMode || 0;
27 var noBottomFold = $.browser.msie && (($.browser.version < 8 && !mode) || mode < 8);
29 var expr = $.browser.msie && (function() {
30 var div = document.createElement('div');
31 try { div.style.setExpression('width','0+0'); div.style.removeExpression('width'); }
32 catch(e) { return false; }
37 return parseInt($.css(el,p))||0;
40 var s = parseInt(s).toString(16);
41 return ( s.length < 2 ) ? '0'+s : s;
44 for ( ; node && node.nodeName.toLowerCase() != 'html'; node = node.parentNode ) {
45 var v = $.css(node,'backgroundColor');
46 if (v == 'rgba(0, 0, 0, 0)')
48 if (v.indexOf('rgb') >= 0) {
49 var rgb = v.match(/\d+/g);
50 return '#'+ hex2(rgb[0]) + hex2(rgb[1]) + hex2(rgb[2]);
52 if ( v && v != 'transparent' )
58 function getWidth(fx, i, width) {
60 case 'round': return Math.round(width*(1-Math.cos(Math.asin(i/width))));
61 case 'cool': return Math.round(width*(1+Math.cos(Math.asin(i/width))));
62 case 'sharp': return Math.round(width*(1-Math.cos(Math.acos(i/width))));
63 case 'bite': return Math.round(width*(Math.cos(Math.asin((width-i-1)/width))));
64 case 'slide': return Math.round(width*(Math.atan2(i,width/i)));
65 case 'jut': return Math.round(width*(Math.atan2(width,(width-i-1))));
66 case 'curl': return Math.round(width*(Math.atan(i)));
67 case 'tear': return Math.round(width*(Math.cos(i)));
68 case 'wicked': return Math.round(width*(Math.tan(i)));
69 case 'long': return Math.round(width*(Math.sqrt(i)));
70 case 'sculpt': return Math.round(width*(Math.log((width-i-1),width)));
72 case 'dog': return (i&1) ? (i+1) : width;
73 case 'dog2': return (i&2) ? (i+1) : width;
74 case 'dog3': return (i&3) ? (i+1) : width;
75 case 'fray': return (i%2)*width;
76 case 'notch': return width;
78 case 'bevel': return i+1;
82 $.fn.corner = function(options) {
83 // in 1.3+ we can fix mistakes with the ready state
84 if (this.length == 0) {
85 if (!$.isReady && this.selector) {
86 var s = this.selector, c = this.context;
88 $(s,c).corner(options);
94 return this.each(function(index){
96 // meta values override options
97 var o = [$this.attr($.fn.corner.defaults.metaAttr) || '', options || ''].join(' ').toLowerCase();
98 var keep = /keep/.test(o); // keep borders?
99 var cc = ((o.match(/cc:(#[0-9a-f]+)/)||[])[1]); // corner color
100 var sc = ((o.match(/sc:(#[0-9a-f]+)/)||[])[1]); // strip color
101 var width = parseInt((o.match(/(\d+)px/)||[])[1]) || 10; // corner width
102 var re = /round|bevelfold|bevel|notch|bite|cool|sharp|slide|jut|curl|tear|fray|wicked|sculpt|long|dog3|dog2|dogfold|dog/;
103 var fx = ((o.match(re)||['round'])[0]);
104 var fold = /dogfold|bevelfold/.test(o);
105 var edges = { T:0, B:1 };
107 TL: /top|tl|left/.test(o), TR: /top|tr|right/.test(o),
108 BL: /bottom|bl|left/.test(o), BR: /bottom|br|right/.test(o)
110 if ( !opts.TL && !opts.TR && !opts.BL && !opts.BR )
111 opts = { TL:1, TR:1, BL:1, BR:1 };
113 // support native rounding
114 if ($.fn.corner.defaults.useNative && fx == 'round' && (radius || moz || webkit) && !cc && !sc) {
116 $this.css(radius ? 'border-top-left-radius' : moz ? '-moz-border-radius-topleft' : '-webkit-border-top-left-radius', width + 'px');
118 $this.css(radius ? 'border-top-right-radius' : moz ? '-moz-border-radius-topright' : '-webkit-border-top-right-radius', width + 'px');
120 $this.css(radius ? 'border-bottom-left-radius' : moz ? '-moz-border-radius-bottomleft' : '-webkit-border-bottom-left-radius', width + 'px');
122 $this.css(radius ? 'border-bottom-right-radius' : moz ? '-moz-border-radius-bottomright' : '-webkit-border-bottom-right-radius', width + 'px');
126 var strip = document.createElement('div');
132 backgroundColor: sc || 'transparent',
137 T: parseInt($.css(this,'paddingTop'))||0, R: parseInt($.css(this,'paddingRight'))||0,
138 B: parseInt($.css(this,'paddingBottom'))||0, L: parseInt($.css(this,'paddingLeft'))||0
141 if (typeof this.style.zoom != undefined) this.style.zoom = 1; // force 'hasLayout' in IE
142 if (!keep) this.style.border = 'none';
143 strip.style.borderColor = cc || gpc(this.parentNode);
144 var cssHeight = $.curCSS(this, 'height');
146 for (var j in edges) {
148 // only add stips if needed
149 if ((bot && (opts.BL || opts.BR)) || (!bot && (opts.TL || opts.TR))) {
150 strip.style.borderStyle = 'none '+(opts[j+'R']?'solid':'none')+' none '+(opts[j+'L']?'solid':'none');
151 var d = document.createElement('div');
152 $(d).addClass('jquery-corner');
155 bot ? this.appendChild(d) : this.insertBefore(d, this.firstChild);
157 if (bot && cssHeight != 'auto') {
158 if ($.css(this,'position') == 'static')
159 this.style.position = 'relative';
160 ds.position = 'absolute';
161 ds.bottom = ds.left = ds.padding = ds.margin = '0';
163 ds.setExpression('width', 'this.parentNode.offsetWidth');
167 else if (!bot && $.browser.msie) {
168 if ($.css(this,'position') == 'static')
169 this.style.position = 'relative';
170 ds.position = 'absolute';
171 ds.top = ds.left = ds.right = ds.padding = ds.margin = '0';
173 // fix ie6 problem when blocked element has a border width
175 var bw = sz(this,'borderLeftWidth') + sz(this,'borderRightWidth');
176 ds.setExpression('width', 'this.parentNode.offsetWidth - '+bw+'+ "px"');
182 ds.position = 'relative';
183 ds.margin = !bot ? '-'+pad.T+'px -'+pad.R+'px '+(pad.T-width)+'px -'+pad.L+'px' :
184 (pad.B-width)+'px -'+pad.R+'px -'+pad.B+'px -'+pad.L+'px';
187 for (var i=0; i < width; i++) {
188 var w = Math.max(0,getWidth(fx,i, width));
189 var e = strip.cloneNode(false);
190 e.style.borderWidth = '0 '+(opts[j+'R']?w:0)+'px 0 '+(opts[j+'L']?w:0)+'px';
191 bot ? d.appendChild(e) : d.insertBefore(e, d.firstChild);
194 if (fold && $.support.boxModel) {
195 if (bot && noBottomFold) continue;
196 for (var c in opts) {
197 if (!opts[c]) continue;
198 if (bot && (c == 'TL' || c == 'TR')) continue;
199 if (!bot && (c == 'BL' || c == 'BR')) continue;
201 var common = { position: 'absolute', border: 'none', margin: 0, padding: 0, overflow: 'hidden', backgroundColor: strip.style.borderColor };
202 var $horz = $('<div/>').css(common).css({ width: width + 'px', height: '1px' });
204 case 'TL': $horz.css({ bottom: 0, left: 0 }); break;
205 case 'TR': $horz.css({ bottom: 0, right: 0 }); break;
206 case 'BL': $horz.css({ top: 0, left: 0 }); break;
207 case 'BR': $horz.css({ top: 0, right: 0 }); break;
209 d.appendChild($horz[0]);
211 var $vert = $('<div/>').css(common).css({ top: 0, bottom: 0, width: '1px', height: width + 'px' });
213 case 'TL': $vert.css({ left: width }); break;
214 case 'TR': $vert.css({ right: width }); break;
215 case 'BL': $vert.css({ left: width }); break;
216 case 'BR': $vert.css({ right: width }); break;
218 d.appendChild($vert[0]);
226 $.fn.uncorner = function() {
227 if (radius || moz || webkit)
228 this.css(radius ? 'border-radius' : moz ? '-moz-border-radius' : '-webkit-border-radius', 0);
229 $('div.jquery-corner', this).remove();
234 $.fn.corner.defaults = {
235 useNative: true, // true if plugin should attempt to use native browser support for border radius rounding
236 metaAttr: 'data-corner' // name of meta attribute to use for options