ultimix
jquery.corner.js
Go to the documentation of this file.
1 
20 ;(function($) {
21 
22 var style = document.createElement('div').style,
23  moz = style['MozBorderRadius'] !== undefined,
24  webkit = style['WebkitBorderRadius'] !== undefined,
25  radius = style['borderRadius'] !== undefined || style['BorderRadius'] !== undefined,
26  mode = document.documentMode || 0,
27  noBottomFold = $.browser.msie && (($.browser.version < 8 && !mode) || mode < 8),
28 
29  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; }
33  return true;
34  })();
35 
36 $.support = $.support || {};
37 $.support.borderRadius = moz || webkit || radius; // so you can do: if (!$.support.borderRadius) $('#myDiv').corner();
38 
39 function sz(el, p) {
40  return parseInt($.css(el,p))||0;
41 };
42 function hex2(s) {
43  s = parseInt(s).toString(16);
44  return ( s.length < 2 ) ? '0'+s : s;
45 };
46 function gpc(node) {
47  while(node) {
48  var v = $.css(node,'backgroundColor'), rgb;
49  if (v && v != 'transparent' && v != 'rgba(0, 0, 0, 0)') {
50  if (v.indexOf('rgb') >= 0) {
51  rgb = v.match(/\d+/g);
52  return '#'+ hex2(rgb[0]) + hex2(rgb[1]) + hex2(rgb[2]);
53  }
54  return v;
55  }
56  if (node.nodeName.toLowerCase() == 'html')
57  break;
58  node = node.parentNode; // keep walking if transparent
59  }
60  return '#ffffff';
61 };
62 
63 function getWidth(fx, i, width) {
64  switch(fx) {
65  case 'round': return Math.round(width*(1-Math.cos(Math.asin(i/width))));
66  case 'cool': return Math.round(width*(1+Math.cos(Math.asin(i/width))));
67  case 'sharp': return width-i;
68  case 'bite': return Math.round(width*(Math.cos(Math.asin((width-i-1)/width))));
69  case 'slide': return Math.round(width*(Math.atan2(i,width/i)));
70  case 'jut': return Math.round(width*(Math.atan2(width,(width-i-1))));
71  case 'curl': return Math.round(width*(Math.atan(i)));
72  case 'tear': return Math.round(width*(Math.cos(i)));
73  case 'wicked': return Math.round(width*(Math.tan(i)));
74  case 'long': return Math.round(width*(Math.sqrt(i)));
75  case 'sculpt': return Math.round(width*(Math.log((width-i-1),width)));
76  case 'dogfold':
77  case 'dog': return (i&1) ? (i+1) : width;
78  case 'dog2': return (i&2) ? (i+1) : width;
79  case 'dog3': return (i&3) ? (i+1) : width;
80  case 'fray': return (i%2)*width;
81  case 'notch': return width;
82  case 'bevelfold':
83  case 'bevel': return i+1;
84  case 'steep': return i/2 + 1;
85  case 'invsteep':return (width-i)/2+1;
86  }
87 };
88 
89 $.fn.corner = function(options) {
90  // in 1.3+ we can fix mistakes with the ready state
91  if (this.length == 0) {
92  if (!$.isReady && this.selector) {
93  var s = this.selector, c = this.context;
94  $(function() {
95  $(s,c).corner(options);
96  });
97  }
98  return this;
99  }
100 
101  return this.each(function(index){
102  var $this = $(this),
103  // meta values override options
104  o = [$this.attr($.fn.corner.defaults.metaAttr) || '', options || ''].join(' ').toLowerCase(),
105  keep = /keep/.test(o), // keep borders?
106  cc = ((o.match(/cc:(#[0-9a-f]+)/)||[])[1]), // corner color
107  sc = ((o.match(/sc:(#[0-9a-f]+)/)||[])[1]), // strip color
108  width = parseInt((o.match(/(\d+)px/)||[])[1]) || 10, // corner width
109  re = /round|bevelfold|bevel|notch|bite|cool|sharp|slide|jut|curl|tear|fray|wicked|sculpt|long|dog3|dog2|dogfold|dog|invsteep|steep/,
110  fx = ((o.match(re)||['round'])[0]),
111  fold = /dogfold|bevelfold/.test(o),
112  edges = { T:0, B:1 },
113  opts = {
114  TL: /top|tl|left/.test(o), TR: /top|tr|right/.test(o),
115  BL: /bottom|bl|left/.test(o), BR: /bottom|br|right/.test(o)
116  },
117  // vars used in func later
118  strip, pad, cssHeight, j, bot, d, ds, bw, i, w, e, c, common, $horz;
119 
120  if ( !opts.TL && !opts.TR && !opts.BL && !opts.BR )
121  opts = { TL:1, TR:1, BL:1, BR:1 };
122 
123  // support native rounding
124  if ($.fn.corner.defaults.useNative && fx == 'round' && (radius || moz || webkit) && !cc && !sc) {
125  if (opts.TL)
126  $this.css(radius ? 'border-top-left-radius' : moz ? '-moz-border-radius-topleft' : '-webkit-border-top-left-radius', width + 'px');
127  if (opts.TR)
128  $this.css(radius ? 'border-top-right-radius' : moz ? '-moz-border-radius-topright' : '-webkit-border-top-right-radius', width + 'px');
129  if (opts.BL)
130  $this.css(radius ? 'border-bottom-left-radius' : moz ? '-moz-border-radius-bottomleft' : '-webkit-border-bottom-left-radius', width + 'px');
131  if (opts.BR)
132  $this.css(radius ? 'border-bottom-right-radius' : moz ? '-moz-border-radius-bottomright' : '-webkit-border-bottom-right-radius', width + 'px');
133  return;
134  }
135 
136  strip = document.createElement('div');
137  $(strip).css({
138  overflow: 'hidden',
139  height: '1px',
140  minHeight: '1px',
141  fontSize: '1px',
142  backgroundColor: sc || 'transparent',
143  borderStyle: 'solid'
144  });
145 
146  pad = {
147  T: parseInt($.css(this,'paddingTop'))||0, R: parseInt($.css(this,'paddingRight'))||0,
148  B: parseInt($.css(this,'paddingBottom'))||0, L: parseInt($.css(this,'paddingLeft'))||0
149  };
150 
151  if (typeof this.style.zoom != undefined) this.style.zoom = 1; // force 'hasLayout' in IE
152  if (!keep) this.style.border = 'none';
153  strip.style.borderColor = cc || gpc(this.parentNode);
154  cssHeight = $(this).outerHeight();
155 
156  for (j in edges) {
157  bot = edges[j];
158  // only add stips if needed
159  if ((bot && (opts.BL || opts.BR)) || (!bot && (opts.TL || opts.TR))) {
160  strip.style.borderStyle = 'none '+(opts[j+'R']?'solid':'none')+' none '+(opts[j+'L']?'solid':'none');
161  d = document.createElement('div');
162  $(d).addClass('jquery-corner');
163  ds = d.style;
164 
165  bot ? this.appendChild(d) : this.insertBefore(d, this.firstChild);
166 
167  if (bot && cssHeight != 'auto') {
168  if ($.css(this,'position') == 'static')
169  this.style.position = 'relative';
170  ds.position = 'absolute';
171  ds.bottom = ds.left = ds.padding = ds.margin = '0';
172  if (expr)
173  ds.setExpression('width', 'this.parentNode.offsetWidth');
174  else
175  ds.width = '100%';
176  }
177  else if (!bot && $.browser.msie) {
178  if ($.css(this,'position') == 'static')
179  this.style.position = 'relative';
180  ds.position = 'absolute';
181  ds.top = ds.left = ds.right = ds.padding = ds.margin = '0';
182 
183  // fix ie6 problem when blocked element has a border width
184  if (expr) {
185  bw = sz(this,'borderLeftWidth') + sz(this,'borderRightWidth');
186  ds.setExpression('width', 'this.parentNode.offsetWidth - '+bw+'+ "px"');
187  }
188  else
189  ds.width = '100%';
190  }
191  else {
192  ds.position = 'relative';
193  ds.margin = !bot ? '-'+pad.T+'px -'+pad.R+'px '+(pad.T-width)+'px -'+pad.L+'px' :
194  (pad.B-width)+'px -'+pad.R+'px -'+pad.B+'px -'+pad.L+'px';
195  }
196 
197  for (i=0; i < width; i++) {
198  w = Math.max(0,getWidth(fx,i, width));
199  e = strip.cloneNode(false);
200  e.style.borderWidth = '0 '+(opts[j+'R']?w:0)+'px 0 '+(opts[j+'L']?w:0)+'px';
201  bot ? d.appendChild(e) : d.insertBefore(e, d.firstChild);
202  }
203 
204  if (fold && $.support.boxModel) {
205  if (bot && noBottomFold) continue;
206  for (c in opts) {
207  if (!opts[c]) continue;
208  if (bot && (c == 'TL' || c == 'TR')) continue;
209  if (!bot && (c == 'BL' || c == 'BR')) continue;
210 
211  common = { position: 'absolute', border: 'none', margin: 0, padding: 0, overflow: 'hidden', backgroundColor: strip.style.borderColor };
212  $horz = $('<div/>').css(common).css({ width: width + 'px', height: '1px' });
213  switch(c) {
214  case 'TL': $horz.css({ bottom: 0, left: 0 }); break;
215  case 'TR': $horz.css({ bottom: 0, right: 0 }); break;
216  case 'BL': $horz.css({ top: 0, left: 0 }); break;
217  case 'BR': $horz.css({ top: 0, right: 0 }); break;
218  }
219  d.appendChild($horz[0]);
220 
221  var $vert = $('<div/>').css(common).css({ top: 0, bottom: 0, width: '1px', height: width + 'px' });
222  switch(c) {
223  case 'TL': $vert.css({ left: width }); break;
224  case 'TR': $vert.css({ right: width }); break;
225  case 'BL': $vert.css({ left: width }); break;
226  case 'BR': $vert.css({ right: width }); break;
227  }
228  d.appendChild($vert[0]);
229  }
230  }
231  }
232  }
233  });
234 };
235 
236 $.fn.uncorner = function() {
237  if (radius || moz || webkit)
238  this.css(radius ? 'border-radius' : moz ? '-moz-border-radius' : '-webkit-border-radius', 0);
239  $('div.jquery-corner', this).remove();
240  return this;
241 };
242 
243 // expose options
244 $.fn.corner.defaults = {
245  useNative: true, // true if plugin should attempt to use native browser support for border radius rounding
246  metaAttr: 'data-corner' // name of meta attribute to use for options
247 };
248 
249 })(jQuery);