OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / scripts / jquery / jquery.corner.js
1 /*!
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
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 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);
28
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; }
33     return true;
34 })();
35     
36 function sz(el, p) { 
37     return parseInt($.css(el,p))||0; 
38 };
39 function hex2(s) {
40     var s = parseInt(s).toString(16);
41     return ( s.length < 2 ) ? '0'+s : s;
42 };
43 function gpc(node) {
44     for ( ; node && node.nodeName.toLowerCase() != 'html'; node = node.parentNode ) {
45         var v = $.css(node,'backgroundColor');
46         if (v == 'rgba(0, 0, 0, 0)')
47             continue; // webkit
48         if (v.indexOf('rgb') >= 0) { 
49             var rgb = v.match(/\d+/g); 
50             return '#'+ hex2(rgb[0]) + hex2(rgb[1]) + hex2(rgb[2]);
51         }
52         if ( v && v != 'transparent' )
53             return v;
54     }
55     return '#ffffff';
56 };
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 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)));
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     }
80 };
81
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;
87             $(function() {
88                 $(s,c).corner(options);
89             });
90         }
91         return this;
92         }
93
94     return this.each(function(index){
95                 var $this = $(this);
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 };
106                 var 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                 if ( !opts.TL && !opts.TR && !opts.BL && !opts.BR )
111                         opts = { TL:1, TR:1, BL:1, BR:1 };
112                         
113                 // support native rounding
114                 if ($.fn.corner.defaults.useNative && fx == 'round' && (radius || moz || webkit) && !cc && !sc) {
115                         if (opts.TL)
116                                 $this.css(radius ? 'border-top-left-radius' : moz ? '-moz-border-radius-topleft' : '-webkit-border-top-left-radius', width + 'px');
117                         if (opts.TR)
118                                 $this.css(radius ? 'border-top-right-radius' : moz ? '-moz-border-radius-topright' : '-webkit-border-top-right-radius', width + 'px');
119                         if (opts.BL)
120                                 $this.css(radius ? 'border-bottom-left-radius' : moz ? '-moz-border-radius-bottomleft' : '-webkit-border-bottom-left-radius', width + 'px');
121                         if (opts.BR)
122                                 $this.css(radius ? 'border-bottom-right-radius' : moz ? '-moz-border-radius-bottomright' : '-webkit-border-bottom-right-radius', width + 'px');
123                         return;
124                 }
125                         
126                 var 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         
136         var pad = {
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
139         };
140
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');
145
146         for (var j in edges) {
147             var bot = edges[j];
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');
153                 var ds = d.style;
154
155                 bot ? this.appendChild(d) : this.insertBefore(d, this.firstChild);
156
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';
162                     if (expr)
163                         ds.setExpression('width', 'this.parentNode.offsetWidth');
164                     else
165                         ds.width = '100%';
166                 }
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';
172                     
173                     // fix ie6 problem when blocked element has a border width
174                     if (expr) {
175                         var bw = sz(this,'borderLeftWidth') + sz(this,'borderRightWidth');
176                         ds.setExpression('width', 'this.parentNode.offsetWidth - '+bw+'+ "px"');
177                     }
178                     else
179                         ds.width = '100%';
180                 }
181                 else {
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';                
185                 }
186
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);
192                 }
193                                 
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;
200                                                 
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' });
203                                                 switch(c) {
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;
208                                                 }
209                                                 d.appendChild($horz[0]);
210                                                 
211                                                 var $vert = $('<div/>').css(common).css({ top: 0, bottom: 0, width: '1px', height: width + 'px' });
212                                                 switch(c) {
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;
217                                                 }
218                                                 d.appendChild($vert[0]);
219                                         }
220                                 }
221             }
222         }
223     });
224 };
225
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();
230         return this;
231 };
232
233 // expose options
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
237 };
238     
239 })(jQuery);