You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.corner.js 11KB

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