【问题标题】:How can I test for clip-path support?如何测试剪辑路径支持?
【发布时间】:2014-12-19 02:40:32
【问题描述】:

clip-path:shape() 似乎在 IE(不足为奇)和 Firefox(有点惊讶)中不起作用。有没有办法测试剪辑路径支持?我使用现代化。 (顺便说一句,我知道我可以使用 SVG 和 -webkit-clip-path:url(#mySVG) 来实现这一点)

【问题讨论】:

  • hahahah.... IE 和 Firefox 不是 webkit... webkit 仅支持 - 等待它 - webkit 浏览器。即 Chrome(预闪烁)和 Safari。 除此之外,我没有有用的答案。 耸耸肩
  • 很有趣。我应该说“剪辑路径”而不是 webkit。然而,我似乎看不到浏览器测试剪辑路径支持的方法。
  • 谷歌搜索...找到modernizr.com/docs/#features-misc...有帮助吗?
  • 是的,我也看到了。测试 SVG Clip 路径支持,它为 Firefox 返回 true(这是正确的)。但是 firefox 不支持 clip-path:circle() 只支持 clip-path:url(#mySVG)。无论如何,我认为我现在的问题毫无意义。我只需要测试 Firefox 和 IE,因为这两个浏览器不支持它。
  • 是的,不得不这样做很糟糕(那里有很多测试能力而不是用户代理的纳粹分子),但你必须做你必须做的事情。

标签: css cross-browser modernizr


【解决方案1】:

你刚才问过这个问题,老实说,我不确定 Modernizr 是否还没有添加对此的支持,但在这种情况下很容易推出你自己的测试。

步骤如下:

  1. 创建但不附加 DOM 元素。
  2. 通过检查新创建元素的 JS style 属性(element.style.clipPath === '' 如果可以支持)来检查它是否支持任何类型的 clipPath。
  3. 通过使element.style.clipPath 等于某些有效的 CSS 剪辑路径形状来检查它是否支持 CSS 剪辑路径形状。

当然,它比这要复杂一些,因为您必须检查供应商特定的前缀。

这一切都在一起了:

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

【讨论】:

  • 这应该被添加到modernizr。我在 IE 中进行了测试,它正确检测到不支持 svg 剪辑路径(在 HTML 元素上)。谢谢!
  • 不幸的是,这表明 Edge 18 支持剪辑路径,但实际上并不支持。
  • 如果您将最后一个测试从 testElement.style[property] !== '' 更改为 testElement.style[property].indexOf('polygon') !== -1 它可以工作。 Edge 将属性值设置为 'none' 而不是空字符串。如果您不想将 Modernizr 包含在一个简单的案例中,我喜欢它作为较小的替代品。
【解决方案2】:

您可以使用 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);

Check this codepen 看看它的实际效果。

【讨论】:

  • 这太好了,谢谢!您是否找到了一种方法来避免 IE 在 HTML 元素的 URL 剪辑路径上出现误报?
猜你喜欢
  • 2020-09-06
  • 2020-03-16
  • 2021-11-28
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
  • 2021-02-25
  • 2013-03-31
  • 2016-09-18
相关资源
最近更新 更多