Corners

Let's you add corners

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/7794/34268/Corners.js

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