2011-08-27 47 views
13

我在我的網站上顯示一個元素,我旋轉-90deg 但如果瀏覽器不支持CSS變換元素 看起來錯位並且不太好。 現在我想檢測與JavaScript或jQuery(它是無差別的 如果jQ或JS,因爲我使用/已加載jQ在我的網站上)如果通過CSS這種旋轉支持?使用JavaScript或jQuery檢測,如果CSS變換2D可用

我知道Modernizr但只是爲了那件小事我不想包括整個庫(並加快網站速度加載速度)。

+2

看一看源[jQuery的變換插件](https://github.com/louisremi/jquery.transform.js/)(它擴展了jQuery'.css()'和'.animate() '功能)他們是如何做到的。或者,更好的是,只需使用該插件:)它很小。 – BalusC

+1

您不必從modernizr下載整個包,您可以從自定義下載中選擇第2個轉換 –

+0

我只是看着modernizr如何做事,並從中爲我的項目借用了小塊。 – jfriend00

回答

0

只要把你需要什麼了Modernizr的

首先我們需要的testProps功能

/** 
    * testProps is a generic CSS/DOM property test; if a browser supports 
    * a certain property, it won't return undefined for it. 
    * A supported CSS property returns empty string when its not yet set. 
    */ 
    function testProps(props, prefixed) { 
     for (var i in props) { 
      if (mStyle[ props[i] ] !== undefined) { 
       return prefixed == 'pfx' ? props[i] : true; 
      } 
     } 
     return false; 
    } 

然後運行cssTransform測試

var tests = []; 
tests['csstransforms'] = function() { 
     return !!testProps(['transformProperty', 'WebkitTransform', 'MozTransform', 'OTransform', 'msTransform']); 
    }; 

如果測試[ 'csstransforms']是真實的,那麼你有這個功能可用。

+0

代碼似乎有一個錯誤:'mStyle未定義' – Poru

+0

定義它然後:) var myStyle = [] – wesbos

+6

我不認爲你可以只是定義它...看起來石灰mStyle是一個全球性的瀏覽器支持css道具在那裏....如果你必須定義該變量,它將永遠是假的,這個函數是完全沒用的... –

4

這就像你得到一個簡單的和一個jsfiddle。它不再進行無限循環。

function getSupportedTransform() { 
    var prefixes = 'transform WebkitTransform MozTransform OTransform msTransform'.split(' '); 
    for(var i = 0; i < prefixes.length; i++) { 
     if(document.createElement('div').style[prefixes[i]] !== undefined) { 
      return prefixes[i]; 
     } 
    } 
    return false; 
} 
+8

這個去如果瀏覽器不支持轉換(也會吃掉所有內存),則會在無限循環中運行。 – maxgalbu

+1

但它有5 upvotes! – Dan

+1

'el'變量的用途是什麼?我想你不想在每次傳遞中都創建div元素,但是你沒有使用它。顯然,如果沒有前綴可用,無限循環。 – Sinisa

2

下面是我使用來檢測,如果CSS3過渡支持代碼:

var div = document.createElement('div'); 
div.setAttribute('style', 'transition:top 1s ease;-webkit-transition:top 1s ease;-moz-transition:top 1s ease;-o-transition:top 1s ease;'); 
document.body.appendChild(div); 
var cssTransitionsSupported = !!(div.style.transition || div.style.webkitTransition || div.style.MozTransition || div.style.OTransitionDuration); 

div.parentNode.removeChild(div); 
div = null; 

我故意不找微軟的支持,因爲微軟還沒有發貨,支持CSS3瀏覽器轉換,我不希望我的代碼自動支持我未來還未測試的實現。

30

這是一個基於Liam答案的函數。如果沒有前綴支持,它將返回第一個支持的前綴的名稱或false

function getSupportedTransform() { 
    var prefixes = 'transform WebkitTransform MozTransform OTransform msTransform'.split(' '); 
    var div = document.createElement('div'); 
    for(var i = 0; i < prefixes.length; i++) { 
     if(div && div.style[prefixes[i]] !== undefined) { 
      return prefixes[i]; 
     } 
    } 
    return false; 
} 
+1

不錯,投票率最高。只是建議在循環外部創建'div'元素。 – Sinisa

+0

@Sinisa很好的電話。相應編輯。 – Roshambo

0

此代碼測試爲2D變換支持。

它可以很容易地調整來檢測支持3D轉換。只需添加'translateZ(1)'即可測試CSS(請參閱源代碼中的defaultTestValues)。

此代碼的優點是它檢測到支持的供應商前綴(如果有的話)。說它:

testCSSSupport('transform') 

可能的返回值:

false,當功能不支持,或

{ 
    vendor: 'moz', 
    cssStyle: '-moz-transform', 
    jsStyle: 'MozTransform' 
} 

時功能支持

/** 
* Test for CSS3 feature support. Single-word properties only by now. 
* This function is not generic, but it works well for transition and transform at least 
*/ 
testCSSSupport: function (feature, cssTestValue/* optional */) { 
    var testDiv, 
     featureCapital = feature.charAt(0).toUpperCase() + feature.substr(1), 
     vendors = ['', 'webkit', 'moz', 'ms'], 
     jsPrefixes = ['', 'Webkit', 'Moz', 'ms'], 
     defaultTestValues = { 
      transform: 'translateX(.5em)' 
      // This will test for 2D transform support 
      // Add translateZ(1) to test 3D transform 
     }, 
     testFunctions = { 
      transform: function (jsProperty, computed) { 
       return computed[jsProperty].substr(0, 9) === 'matrix3d('; 
      } 
     }; 

    function isStyleSupported(feature, jsPrefixedProperty) { 
     if (jsPrefixedProperty in testDiv.style) { 
      var testVal = cssTestValue || defaultTestValues[feature], 
       testFn = testFunctions[feature]; 
      if (!testVal) { 
       return false; 
      } 

      //Assume browser without getComputedStyle is either IE8 or something even more poor 
      if (!window.getComputedStyle) { 
       return false; 
      } 

      testDiv.style[jsPrefixedProperty] = testVal; 
      var computed = window.getComputedStyle(testDiv); 

      if (testFn) { 
       return testFn(jsPrefixedProperty, computed); 
      } 
      else { 
       return computed[jsPrefixedProperty] === testVal; 
      } 
     } 
    } 

    var cssPrefixedProperty, 
     jsPrefixedProperty, 
     testDiv = document.createElement('div'); 

    for (var i = 0; i < vendors.length; i++) { 
     if (i === 0) { 
      cssPrefixedProperty = feature; //todo: this code now works for single-word features only! 
      jsPrefixedProperty = feature; //therefore box-sizing -> boxSizing won't work here 
     } 
     else { 
      cssPrefixedProperty = '-' + vendors[i] + '-' + feature; 
      jsPrefixedProperty = jsPrefixes[i] + featureCapital; 
     } 

     if (isStyleSupported(feature, jsPrefixedProperty)) { 
      return { 
       vendor: vendors[i], 
       cssStyle: cssPrefixedProperty, 
       jsStyle: jsPrefixedProperty 
      }; 
     } 
    } 

    return false; 
}