[f]添加cssTransform属性的方法

 

添加css3的一些Transform的属性

使用方法:

cssTransform(oDiv[0], ‘scale‘, 300)(‘rotate‘, 300);

ps:可以完成一个变形继续下一个变形;

函数如下:

function cssTransform(obj, attr, value) {
   if (arguments.length == 2) {
       if (attr == ‘scale‘ || attr == ‘rotate‘ || attr == ‘rotateY‘ || attr == ‘translateZ‘) {
           var arr = ["Webkit", "Moz", "O", "ms", ""];
           var sVal = ‘‘;
           if (!obj.$Transform) {
               obj.$Transform = {};
           }
           for (var i = 0; i < arr.length; i++) {
               obj.style[arr[i] + "Transform"] = sVal;
               switch (attr) {
                   case ‘scale‘:
                       sVal = typeof(obj.$Transform[attr]) == ‘number‘ ? obj.$Transform[attr] : 100;
                       break;
                   default:
                       sVal = obj.$Transform[attr] ? obj.$Transform[attr] : 0;
               }
           }



       }
       var sCur = obj.currentStyle ? obj.currentStyle[attr] : document.defaultView.getComputedStyle(obj, false)[attr];
       if (attr == ‘opacity‘) {
           return parseInt(parseFloat(sCur) * 100);
       } else {
           return parseInt(sCur);
       }
   } else
   if (arguments.length == 3) {
       switch (attr) {
           case ‘scale‘:
           case ‘rotate‘:
           case ‘rotateY‘:
           case ‘translateZ‘:
               setCss3(obj, attr, value);
               break;
           case ‘width‘:
           case ‘height‘:
           case ‘paddingLeft‘:
           case ‘paddingTop‘:
           case ‘paddingRight‘:
           case ‘paddingBottom‘:
               value = Math.max(value, 0);
           case ‘left‘:
           case ‘top‘:
           case ‘marginLeft‘:
           case ‘marginTop‘:
           case ‘marginRight‘:
           case ‘marginBottom‘:
               obj.style[attr] = value + ‘px‘;
               break;
           case ‘opacity‘:
               obj.style.filter = "alpha(opacity:" + value + ")";
               obj.style.opacity = value / 100;
               break;
           default:
               obj.style[attr] = value;
       }
   }
   return function(attr_in, value_in) {
       css(obj, attr_in, value_in)
   };
}

function setCss3(obj, attr, value) {
   var sTr = "";
   var sVal = "";
   var arr = ["Webkit", "Moz", "O", "ms", ""];
   if (!obj["$Transform"]) {
       obj["$Transform"] = {};
   }
   obj["$Transform"][attr] = parseInt(value);
   for (sTr in obj["$Transform"]) {
       switch (sTr) {
           case ‘scale‘:
               sVal += sTr + "(" + (obj["$Transform"][sTr] / 100) + ") ";
               break;
           case ‘rotate‘:
           case ‘rotateY‘:
               sVal += sTr + "(" + (obj["$Transform"][sTr]) + "deg) ";
               break;
           case ‘translateZ‘:
               sVal += sTr + "(" + (obj["$Transform"][sTr]) + "px) ";
               break;
       }
   }
   for (var i = 0; i < arr.length; i++) {
       obj.style[arr[i] + "Transform"] = sVal;
   }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。