web/data/jquery.corner.js
changeset 9402 2c48c091b6a2
parent 9127 aff75b69db92
parent 9401 17dc43c1f1bd
child 9403 d81207fb9499
equal deleted inserted replaced
9127:aff75b69db92 9402:2c48c091b6a2
     1 /*!
       
     2  * jQuery corner plugin: simple corner rounding
       
     3  * Examples and documentation at: http://jquery.malsup.com/corner/
       
     4  * version 2.11 (15-JUN-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
       
    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 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     var 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 Math.round(width*(1-Math.cos(Math.acos(i/width))));
       
    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     }
       
    85 };
       
    86 
       
    87 $.fn.corner = function(options) {
       
    88     // in 1.3+ we can fix mistakes with the ready state
       
    89     if (this.length == 0) {
       
    90         if (!$.isReady && this.selector) {
       
    91             var s = this.selector, c = this.context;
       
    92             $(function() {
       
    93                 $(s,c).corner(options);
       
    94             });
       
    95         }
       
    96         return this;
       
    97     }
       
    98 
       
    99     return this.each(function(index){
       
   100         var $this = $(this),
       
   101             // meta values override options
       
   102             o = [$this.attr($.fn.corner.defaults.metaAttr) || '', options || ''].join(' ').toLowerCase(),
       
   103             keep = /keep/.test(o),                       // keep borders?
       
   104             cc = ((o.match(/cc:(#[0-9a-f]+)/)||[])[1]),  // corner color
       
   105             sc = ((o.match(/sc:(#[0-9a-f]+)/)||[])[1]),  // strip color
       
   106             width = parseInt((o.match(/(\d+)px/)||[])[1]) || 10, // corner width
       
   107             re = /round|bevelfold|bevel|notch|bite|cool|sharp|slide|jut|curl|tear|fray|wicked|sculpt|long|dog3|dog2|dogfold|dog/,
       
   108             fx = ((o.match(re)||['round'])[0]),
       
   109             fold = /dogfold|bevelfold/.test(o),
       
   110             edges = { T:0, B:1 },
       
   111             opts = {
       
   112                 TL:  /top|tl|left/.test(o),       TR:  /top|tr|right/.test(o),
       
   113                 BL:  /bottom|bl|left/.test(o),    BR:  /bottom|br|right/.test(o)
       
   114             },
       
   115             // vars used in func later
       
   116             strip, pad, cssHeight, j, bot, d, ds, bw, i, w, e, c, common, $horz;
       
   117 
       
   118         if ( !opts.TL && !opts.TR && !opts.BL && !opts.BR )
       
   119             opts = { TL:1, TR:1, BL:1, BR:1 };
       
   120 
       
   121         // support native rounding
       
   122         if ($.fn.corner.defaults.useNative && fx == 'round' && (radius || moz || webkit) && !cc && !sc) {
       
   123             if (opts.TL)
       
   124                 $this.css(radius ? 'border-top-left-radius' : moz ? '-moz-border-radius-topleft' : '-webkit-border-top-left-radius', width + 'px');
       
   125             if (opts.TR)
       
   126                 $this.css(radius ? 'border-top-right-radius' : moz ? '-moz-border-radius-topright' : '-webkit-border-top-right-radius', width + 'px');
       
   127             if (opts.BL)
       
   128                 $this.css(radius ? 'border-bottom-left-radius' : moz ? '-moz-border-radius-bottomleft' : '-webkit-border-bottom-left-radius', width + 'px');
       
   129             if (opts.BR)
       
   130                 $this.css(radius ? 'border-bottom-right-radius' : moz ? '-moz-border-radius-bottomright' : '-webkit-border-bottom-right-radius', width + 'px');
       
   131             return;
       
   132         }
       
   133 
       
   134         strip = document.createElement('div');
       
   135         $(strip).css({
       
   136             overflow: 'hidden',
       
   137             height: '1px',
       
   138             minHeight: '1px',
       
   139             fontSize: '1px',
       
   140             backgroundColor: sc || 'transparent',
       
   141             borderStyle: 'solid'
       
   142         });
       
   143 
       
   144         pad = {
       
   145             T: parseInt($.css(this,'paddingTop'))||0,     R: parseInt($.css(this,'paddingRight'))||0,
       
   146             B: parseInt($.css(this,'paddingBottom'))||0,  L: parseInt($.css(this,'paddingLeft'))||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 && $.browser.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 
       
   181                     // fix ie6 problem when blocked element has a border width
       
   182                     if (expr) {
       
   183                         bw = sz(this,'borderLeftWidth') + sz(this,'borderRightWidth');
       
   184                         ds.setExpression('width', 'this.parentNode.offsetWidth - '+bw+'+ "px"');
       
   185                     }
       
   186                     else
       
   187                         ds.width = '100%';
       
   188                 }
       
   189                 else {
       
   190                     ds.position = 'relative';
       
   191                     ds.margin = !bot ? '-'+pad.T+'px -'+pad.R+'px '+(pad.T-width)+'px -'+pad.L+'px' :
       
   192                                         (pad.B-width)+'px -'+pad.R+'px -'+pad.B+'px -'+pad.L+'px';
       
   193                 }
       
   194 
       
   195                 for (i=0; i < width; i++) {
       
   196                     w = Math.max(0,getWidth(fx,i, width));
       
   197                     e = strip.cloneNode(false);
       
   198                     e.style.borderWidth = '0 '+(opts[j+'R']?w:0)+'px 0 '+(opts[j+'L']?w:0)+'px';
       
   199                     bot ? d.appendChild(e) : d.insertBefore(e, d.firstChild);
       
   200                 }
       
   201 
       
   202                 if (fold && $.support.boxModel) {
       
   203                     if (bot && noBottomFold) continue;
       
   204                     for (c in opts) {
       
   205                         if (!opts[c]) continue;
       
   206                         if (bot && (c == 'TL' || c == 'TR')) continue;
       
   207                         if (!bot && (c == 'BL' || c == 'BR')) continue;
       
   208 
       
   209                         common = { position: 'absolute', border: 'none', margin: 0, padding: 0, overflow: 'hidden', backgroundColor: strip.style.borderColor };
       
   210                         $horz = $('<div/>').css(common).css({ width: width + 'px', height: '1px' });
       
   211                         switch(c) {
       
   212                         case 'TL': $horz.css({ bottom: 0, left: 0 }); break;
       
   213                         case 'TR': $horz.css({ bottom: 0, right: 0 }); break;
       
   214                         case 'BL': $horz.css({ top: 0, left: 0 }); break;
       
   215                         case 'BR': $horz.css({ top: 0, right: 0 }); break;
       
   216                         }
       
   217                         d.appendChild($horz[0]);
       
   218 
       
   219                         var $vert = $('<div/>').css(common).css({ top: 0, bottom: 0, width: '1px', height: width + 'px' });
       
   220                         switch(c) {
       
   221                         case 'TL': $vert.css({ left: width }); break;
       
   222                         case 'TR': $vert.css({ right: width }); break;
       
   223                         case 'BL': $vert.css({ left: width }); break;
       
   224                         case 'BR': $vert.css({ right: width }); break;
       
   225                         }
       
   226                         d.appendChild($vert[0]);
       
   227                     }
       
   228                 }
       
   229             }
       
   230         }
       
   231     });
       
   232 };
       
   233 
       
   234 $.fn.uncorner = function() {
       
   235     if (radius || moz || webkit)
       
   236         this.css(radius ? 'border-radius' : moz ? '-moz-border-radius' : '-webkit-border-radius', 0);
       
   237     $('div.jquery-corner', this).remove();
       
   238     return this;
       
   239 };
       
   240 
       
   241 // expose options
       
   242 $.fn.corner.defaults = {
       
   243     useNative: true, // true if plugin should attempt to use native browser support for border radius rounding
       
   244     metaAttr:  'data-corner' // name of meta attribute to use for options
       
   245 };
       
   246 
       
   247 })(jQuery);