jQuery ã®cssããããã£æ¡å¼µ(ç¬èªããããã£)ã®ä½ãæ¹
ã¿ããªå¤§å¥½ããjavascriptã©ã¤ãã©ãªã® jQueryã«ã¯ã
$(elem).css({ width: 123 });
ã¨ã
var width = $(elem).css('width');
ã¨ã
$(elem).css('width',123);
ã¨ãã¿ããã«ã .css ã¡ã½ãããå©ç¨ãã¦å¤ãè¨å®ãåå¾ãã§ãã¾ãã
ãã ãæåãã決ãããããã®ã ãã§ã¯ãªãã¦ãèªåã§å¥½ããªããããã£ä½ã£ã¦èªç±ã«è¨å®ãããã§ãããw
ããã§ã¯ãååä½ã£ãã クロスブラウザな回転 㧠rotate ã transform ãå®è£ ããã¨ãã«ä½¿ã£ããã¯ããã¯ã解説ãããã¨æãã¾ãã
ã¯ãã¹ãã©ã¦ã¶ã¼ãªå転ã§ã¯ã rotate çã®ç¬èªããããã£ãå®ç¾©ãã¦ãã¾ãã
rotateã«ãã£ã¦ãèªç±ãªå転è§åº¦ãè¨å®ã§ãã¾ãã
$(elem).css({ rotate: 123 });
ã¨ã
var rotate = $(elem).css('rotate');
ã¨ã
$(elem).css('rotate',123);
ã¨ããã§ãã¾ãã
ã½ã¼ã¹
ã½ã¼ã¹ã³ã¼ãã«ã³ã¡ã³ããå ¥ããªãã解説ãã¦ããã¾ãã
(function ($) { //cssããããã£æ¡å¼µããããã«ã¡ã½ãããããã¯ãã¾ãã //ããã¯ããã¡ã½ãã㯠$.style 㨠$.curCSS ã®2ã¤ã§ãã //ã¾ãã¯ãæ¢åã®ã¡ã½ãããä¿åãã¦ããã¾ãã var jquery_fix_orignal_style = $.style; //$.css() çµç±ã§ä¸»ã«å¤ã®ã»ããæã«å¼ã³åºããã var jquery_fix_orignal_curCSS = $.curCSS; //$.css() çµç±ã§ä¸»ã«å¤ã®ã²ããæã«å¼ã³åºããã //transformãªã©ã®åãã©ã¦ã¶ã«ãã£ã¦ååãéãå¯è½æ§ãããããããã£ãå¹çããæ¤ç´¢ãããã«ãã¼é¢æ°ã§ãã //styleObject æ¤ç´¢ãããªãã¸ã§ã¯ãã® style é¨åã elem.style //styleArgs ããããã£æ¤ç´¢ãªã¹ã['WebkitTransform', 'MozTransform', 'OTransform' , 'transform'] ã¿ãããª. // //æ»ãå¤ ãããã㣠function findstyle_for_crossbrowser(styleObject,styleArgs) { var i ; for( i in styleArgs) { //ãããã£ãå®ç¾©ããã¦ããã調ã¹ã¾ãã if (typeof styleObject[styleArgs[i]] != 'undefined') { //å®ç¾©ããã¦ããã°ããã®ããããã£ãæ¡ç¨ãã¾ãã return styleArgs[i]; } } //ããããã£ãå®ç¾©ããã¦ããªãå ´åã¯ãé©å½ã«ä½æãã¾ãã //ãã®å ´åã¯ãããããã£æ¤ç´¢ãªã¹ãã®æå¾ã«æ¸¡ããããããã£ã§èªåä½æãã¦ãã¾ãã styleObject[styleArgs[i]] = ''; return styleArgs[i]; } //jqueryæ¬ä½ãæ¡å¼µãã¾ã! jQuery.extend({ //$.css() çµç±ã§ä¸»ã«å¤ã®ã»ããæã«å¼ã³åºããã. //elem ãªãã¸ã§ã¯ã //name ããããã£å //value å¤ (px çã®æåãã¤ãããã¦ããå¯è½æ§ãããã¾ã.) // //æ»ãå¤ å¤ style: function( elem, name, value ) { switch(name) { //便å©ãªãªã¬ãªã¬ãããã㣠case 'rotate': //å転 //å転ç¨ã®å°ç¨é¢æ°ãå¼ã³åºãã¾ãã $(elem).rotate(value); //ããã§å¦çã¯çµäºã§ã. return value; case 'scale': //æ¡å¤§ //æ¡å¤§ç¨ã®å°ç¨é¢æ°ãå¼ã³åºãã¾ãã $(elem).scale(value); //ããã§å¦çã¯çµäºã§ã. return value; //transformããããã£ã®å¦ç case 'transform': //ãã©ã¦ã¶ã¼ã«ãã£ã¦å®è£ ãç°ãªãã®ã§ãã©ããå®è£ ããã¦ãããæ¤ç´¢ãã¦ããã¾ãã name = findstyle_for_crossbrowser( elem.style || elem , ['WebkitTransform', 'MozTransform', 'OTransform' , 'transform'] ); //å¦çã jQueryã«æµãã¾ãã //name ãå¤æ´ããã ããããã® case ã®ç®çã§ãã break; } //jQueryæ¬ä½ã®æ¬æ¥ã® $.style ãå¼ã³åºãã¾ãã return jquery_fix_orignal_style.apply(this, [elem, name, value] ); } , //$.css() çµç±ã§ä¸»ã«å¤ã®åå¾æã«å¼ã³åºããã. //elem ãªãã¸ã§ã¯ã //name ããããã£å //force ã³ã¬ä½ï¼ ãã®ã¾ã¾ã®å¤ãã¨ãããè¨ç®ã§æ±ãããï¼ // //æ»ãå¤ å¤ curCSS: function( elem, name, force ) { switch(name) { //便å©ãªãªã¬ãªã¬ãããã㣠case 'rotate': //å転 return $(elem).rotate(); case 'scale': //æ¡å¤§ return $(elem).scale(); //transformããããã£ã®å¦ç case 'transform': //ãã©ã¦ã¶ã¼ã«ãã£ã¦å®è£ ãç°ãªãã®ã§ãã©ããå®è£ ããã¦ãããæ¤ç´¢ãã¦ããã¾ãã name = findstyle_for_crossbrowser( elem.style || elem , ['WebkitTransform', 'MozTransform', 'OTransform' , 'transform'] ); //å¦çã jQueryã«æµãã¾ãã //name ãå¤æ´ããã ããããã® case ã®ç®çã§ãã break; } //jQueryæ¬ä½ã®æ¬æ¥ã® $.curCSS ãå¼ã³åºãã¾ãã return jquery_fix_orignal_curCSS.apply(this, [elem, name, force] ); } }); //å転 //大å転 ã¨ãã±ã¶ãã¯ã¹ãã³ã£ããã®å転ãããªãã«! jQuery.fn.rotate = function(val) { //åå¾ if (typeof val == 'undefined') { if (jQuery.browser.opera) { //operaã ãradã§å ¥ã£ã¦ãã ä¾: rotate(0.17rad) var r = this.css('transform').match(/rotate\((.*?)\)/); return ( r && r[1]) ? Math.round(parseFloat(r[1]) * 180 / Math.PI) : 0; } else { //transformããã¼ã¹ãã. var r = this.css('transform').match(/rotate\((.*?)\)/); return ( r && r[1]) ? parseInt(r[1]) : 0; } } //ã»ãã this.css('transform', this.css('transform').replace(/none|rotate\(.*?\)/, '') + 'rotate(' + parseInt(val) + 'deg)'); return this; }; jQuery.fn.scale = function(val) { //åå¾ if (typeof val == 'undefined') { //transformããã¼ã¹ãã. var r = this.css('transform').match(/scale\((.*?)\)/); return ( r && r[1]) ? parseFloat(r[1]) : 1; } //ã»ãã this.css('transform', this.css('transform').replace(/none|scale\(.*?\)/, '') + 'scale(' + parseFloat(val) + ')'); return this; }; //animate ã¡ã½ããã§ã¢ãã¡ã¼ã·ã§ã³ãããå®ç¾© //å転ã®ã¢ãã¡ã¼ã·ã§ã³ jQuery.fx.step.rotate = function(fx) { if ( fx.state == 0 ) { fx.start = $(fx.elem).rotate(); // fx.end = fx.end; fx.now = fx.start; } $(fx.elem).rotate(fx.now); }; //æ¡å¤§ã®ã¢ãã¡ã¼ã·ã§ã³ jQuery.fx.step.scale = function(fx) { if ( fx.state == 0 ) { fx.start = $(fx.elem).scale(); // fx.end = fx.end; fx.now = fx.start; } $(fx.elem).scale(fx.now); }; })(jQuery);
ã³ãã¯ãã¡ã½ããã®ããã¯ã«ããã¾ãã
jQuery ãå
é¨ã§ä½¿ã£ã¦ããã¡ã½ãããããã¯ãã¦ããã§å¥½ããªãã¨ãããã
ã¾ãã¯ãããããã£åãæ¸ãæããã ããã¦ãç¶ãã¯æ¬æ¥ã®ã¡ã½ããã«ããããã¿ãããªã
jQuery ã£ã¦ ã»ããããã¨ãã¯ã $.style ãªã®ã«ãã²ããããã¨ã㯠$.curCSS ã¨ãååãå¾®å¦ã«æ°ããã¾ããã
ãããããªçµç·¯ããã£ãã®ããªã¨æãã¾ãã
ã¾ãã css 㨠animate ã¡ã½ããã¯ãå¥ç©ãªã®ã§ã animate ã®è¨å®ãããªãã¦ã¯ã ãã§ãã
ã¡ãã£ã¨ã¡ã³ãã¤ã§ããããã
å¤æ§æ§ã確ä¿ããã«ã¯ä»æ¹ãªãã£ãã®ããç¥ãã¾ãããã
ã½ã¼ã¹2
ã¢ãã³ãªãã©ã¦ã¶ã ã¨ããã ãã§å転ãããããã§ãã¾ãã
ã ãã©ãã¢ãã³ã§ã¯ãªã IE ã¯ããå°ã調æ´ãå¿
è¦ã§ãã
#IEãå転ã®åéã®å
é§è
ãªãã ãããã¢ãã³ã§ã¯ãªãã¨ããè¨ãæ¹ã¯ããããæ°é§ããã
//ã¿ããªå¤§å¥½ãIEã§å転ãã. if (jQuery.browser.msie) { //絶対座æ¨ãåå¾ãã. //ãã¼ãã«ãªã©ã®åãã¼ãã®å ´åã 親座æ¨ããã®ç¸å¯¾å¤ã«ãªã£ã¦ãã¾ãããã //DOMã®ããªã¼ããã©ããªãã絶対座æ¨ãæ±ãã. // //element ãªãã¸ã§ã¯ã // //æ»ãå¤ [x,y] çµ¶å¯¾åº§æ¨ function GetAbsoluteXY(element) { var pnode = element; var x = pnode.offsetLeft; var y = pnode.offsetTop; while ( pnode.offsetParent ) { pnode = pnode.offsetParent; x += pnode.offsetLeft; y += pnode.offsetTop; if (pnode != document.body && pnode != document.documentElement) { x -= pnode.scrollLeft; y -= pnode.scrollTop; } } return { x: x, y: y }; } //position: static ã position: absolute ã«å¤æãã. // //element ãªãã¸ã§ã¯ã function StaticToAbsolute(element) { if ( element.currentStyle['position'] != 'static') { return ; } //ä»ã®ãªãã¸ã§ã¯ããæããã¨ãã³ã³ãã³ãããã©ãã¨è½ã¡ã¦ãã¾ããªãããã«ä½ç½ãå ¥ãã. var yohakuDiv = document.createElement('div'); yohakuDiv.style.left = element.style.left; yohakuDiv.style.left = element.style.left; yohakuDiv.style.width = element.style.width; yohakuDiv.style.height = element.style.height; element.parentNode.appendChild(yohakuDiv); //absolute å¤æ. var xy = GetAbsoluteXY(element); element.style.position = 'absolute' ; element.style.left = xy.x ; element.style.top = xy.y ; } //IE㧠transform ãå®è£ ãã. // //element ãªãã¸ã§ã¯ã function IETransform(element){ var r,rx,ry; var m11 = 1; var m12 = 1; var m21 = 1; var m22 = 1; //IEã¯å転ãããã¨ã§ãä»ã®ãªãã¸ã§ã¯ãã«ãå½±é¿ãä¸ãã¦ãã¾ãã //ãã®ããã position: absolute ã«å¤æãã. StaticToAbsolute(element); //é·ããªãã®ã§transformã®å¤ãä¸åº¦ã³ãã¼ãã¦ãã. var transform = element.currentStyle['transform'] || ''; //å転 r = transform.match(/rotate\((.*?)\)/); var rotate = ( r && r[1]) ? parseInt(r[1]) : 0; //0 ã 360ã®ç¯å²ã«åãã. rotate = rotate % 360; if (rotate < 0) rotate = 360 + rotate; var radian= rotate * Math.PI / 180; var cosX =Math.cos(radian); var sinY =Math.sin(radian); m11 *= cosX; m12 *= -sinY; m21 *= sinY; m22 *= cosX; //skew r = transform.match(/skewX\((.*?)\)/); var skewX = ( r && r[1]) ? parseFloat(r[1]) : 0; r = transform.match(/skewY\((.*?)\)/); var skewY = ( r && r[1]) ? parseFloat(r[1]) : 0; r = transform.match(/skew\((.*?)(?:,(.*?))?\)/); if (r) { skewX = (r[1]) ? parseFloat(r[1]) : 0; skewY = (r[2]) ? parseFloat(r[2]) : 0; } //0 ã 360ã®ç¯å²ã«åãã. skewX = skewX % 360; if (skewX < 0) skewX = 360 + skewX; skewY = skewY % 360; if (skewY < 0) skewY = 360 + skewY; // m11 = m11; m12 = m12 + Math.tan(skewX * Math.PI / 180); m21 = m21 + Math.tan(skewY * Math.PI / 180); // m22 = m22; //æ¡å¤§ r = transform.match(/scaleX\((.*?)\)/); rx = ( r && r[1]) ? parseFloat(r[1]) : undefined; r = transform.match(/scaleY\((.*?)\)/); ry = ( r && r[1]) ? parseFloat(r[1]) : undefined; r = transform.match(/scale\((.*?)(?:,(.*?))?\)/); var scaleX = ( r && r[1]) ? parseFloat(r[1]) : (typeof rx != 'undefined' ? rx : 1); var scaleY = ( r && r[2]) ? parseFloat(r[2]) : (typeof ry != 'undefined' ? ry : scaleX); m11 *= scaleX; m12 *= scaleX; m21 *= scaleY; m22 *= scaleY; //ãã£ã«ã¿ã¼ã§å転ã¨æ¡å¤§ç¸®å°ãå ãã¾ãã element.style.filter = (element.style.filter || '').replace(/progid:DXImageTransform\.Microsoft\.Matrix\([^)]*\)/, "" ) + ("progid:DXImageTransform.Microsoft.Matrix(" + "M11=" + m11 + ",M12=" + m12 + ",M21=" + m21 + ",M22=" + m22 + ",FilterType='bilinear',sizingMethod='auto expand')") ; //absoluteæã«ã¯è»¸ãè£æ£ãã¦ãããªãã¨ã ãã ã //ãã©ã¦ã¶ã¨ãã¦è»¸ãããã¦ããã //è¨ç®å¼å ãã¿ http://p2b.jp/200912-CSS3-Transform-for-IE8 //offset*ç³»ã®ãµã¤ãºã¯å転ã«ãã£ã¦çããããã¿ãèæ ®ããããããã var w = parseInt(element.offsetWidth); var h = parseInt(element.offsetHeight); //æ¡å¤§ç¸®å°ãåã. //this.get(0).style.width ã height ã«ã¯æ¡ç¸®ã®å½±é¿ãåããªãå ã®æ°åãå ¥ã£ã¦ãã ow = parseInt(element.style.width) oh = parseInt(element.style.height); var quarter_radian= (rotate%90) * Math.PI / 180; //90度ã§å²ããªãã¨å¤ã«ãªãã!! var quarter_cosX =Math.cos(quarter_radian); var quarter_sinY =Math.sin(quarter_radian); //å転ã®è£æ£ var dx = 1/2 * (w - h * quarter_sinY - w * quarter_cosX); var dy = 1/2 * (h - w * quarter_sinY - h * quarter_cosX); //skewã®è£æ£(rotate ããªããskew ããã¨è£æ£ããããããªãã¾ãã ãããããããªã) dx = dx - ow/2 * Math.tan(skewX * Math.PI / 180); dy = dy - oh/2 * Math.tan(skewY * Math.PI / 180); //æ¡å¤§ã®è£æ£ dx = dx - (((ow*scaleX) - ow) / 2); dy = dy - (((oh*scaleY) - oh) / 2); //DX DY ã§è£æ£ãã¦ãå¹ããªãã¨ããããã®ã§ä¸çªç¢ºå®ãªãã¼ã¸ã³ã§ä¸ãã¾ãã element.style.marginLeft = Math.floor(dx); element.style.marginTop = Math.floor(dy); return(true); } //jquery ãå度ããã¯ãã tranform ã IETransform ã«æµãã¾ãã var jquery_iebug_fix_orignal_style = $.style; jQuery.extend({ //$.css() çµç±ã§ä¸»ã«å¤ã®ã»ããæã«å¼ã³åºããã. style: function( elem, name, value ) { var r = jquery_iebug_fix_orignal_style.apply(this, [elem, name, value] ); switch(name) { case 'transform': IETransform(elem); break; } return r; } }); }
ãããªæãã§ãã
é¢ç½ãç¬èªããããã£ãã§ããã°ãããªã¨æãã¾ãã