clip-path:shape()
在IE中不起作用(这并不奇怪),在Firefox中也有点出乎意料。有没有一种方法来测试对于 clip-path的支持?我使用 modernizr。(顺便说一下,我知道使用 SVG和 -webkit-clip-path:url(#mySVG)
可以使这个效果工作。)
clip-path:shape()
在IE中不起作用(这并不奇怪),在Firefox中也有点出乎意料。有没有一种方法来测试对于 clip-path的支持?我使用 modernizr。(顺便说一下,我知道使用 SVG和 -webkit-clip-path:url(#mySVG)
可以使这个效果工作。)
你之前问过这个问题,说实话,我不确定Modernizr是否已经支持了这个功能,但在这种情况下自己编写测试很容易。
步骤如下:
style
属性(如果它可以支持,则为element.style.clipPath ===''
)来检查它是否支持任何类型的clipPath。element.style.clipPath
设置为一些有效的CSS clip path shape来检查它是否支持CSS clip path shapes。当然,这比上述步骤还要复杂一些,因为您必须检查厂商特定的前缀。
以下是所有步骤的综合:
var areClipPathShapesSupported = function () {
var base = 'clipPath',
prefixes = [ 'webkit', 'moz', 'ms', 'o' ],
properties = [ base ],
testElement = document.createElement( 'testelement' ),
attribute = 'polygon(50% 0%, 0% 100%, 100% 100%)';
// Push the prefixed properties into the array of properties.
for ( var i = 0, l = prefixes.length; i < l; i++ ) {
var prefixedProperty = prefixes[i] + base.charAt( 0 ).toUpperCase() + base.slice( 1 ); // remember to capitalize!
properties.push( prefixedProperty );
}
// Interate over the properties and see if they pass two tests.
for ( var i = 0, l = properties.length; i < l; i++ ) {
var property = properties[i];
// First, they need to even support clip-path (IE <= 11 does not)...
if ( testElement.style[property] === '' ) {
// Second, we need to see what happens when we try to create a CSS shape...
testElement.style[property] = attribute;
if ( testElement.style[property] !== '' ) {
return true;
}
}
}
return false;
};
这是一个 CodePen 的概念验证示例:http://codepen.io/anon/pen/YXyyMJ
testElement.style[property] !== ''
更改为testElement.style[property].indexOf('polygon') !== -1
,它会起作用。 Edge 将属性值设置为“none”而不是空字符串。
我喜欢它作为更小的替代方案,如果您不想在简单情况下包含 Modernizr。 - alex3683您可以使用Modernizr进行测试。
(function (Modernizr) {
// Here are all the values we will test. If you want to use just one or two, comment out the lines of test you don't need.
var tests = [{
name: 'svg',
value: 'url(#test)'
}, // False positive in IE, supports SVG clip-path, but not on HTML element
{
name: 'inset',
value: 'inset(10px 20px 30px 40px)'
}, {
name: 'circle',
value: 'circle(60px at center)'
}, {
name: 'ellipse',
value: 'ellipse(50% 50% at 50% 50%)'
}, {
name: 'polygon',
value: 'polygon(50% 0%, 0% 100%, 100% 100%)'
}
];
var t = 0, name, value, prop;
for (; t < tests.length; t++) {
name = tests[t].name;
value = tests[t].value;
Modernizr.addTest('cssclippath' + name, function () {
// Try using window.CSS.supports
if ('CSS' in window && 'supports' in window.CSS) {
for (var i = 0; i < Modernizr._prefixes.length; i++) {
prop = Modernizr._prefixes[i] + 'clip-path'
if (window.CSS.supports(prop, value)) {
return true;
}
}
return false;
}
// Otherwise, use Modernizr.testStyles and examine the property manually
return Modernizr.testStyles('#modernizr { ' + Modernizr._prefixes.join('clip-path:' + value + '; ') + ' }', function (elem, rule) {
var style = getComputedStyle(elem),
clip = style.clipPath;
if (!clip || clip == "none") {
clip = false;
for (var i = 0; i < Modernizr._domPrefixes.length; i++) {
test = Modernizr._domPrefixes[i] + 'ClipPath';
if (style[test] && style[test] !== "none") {
clip = true;
break;
}
}
}
return Modernizr.testProp('clipPath') && clip;
});
});
}
})(Modernizr);
点击这个codepen链接可以查看它的实际效果。