简体   繁体   English

如何检查SVG <g> 元素支持CSS转换

[英]How to check if svg <g> element supports css transform

Chrome, Firefox and Safari supports tranformation of svg <g> element by CSS style without prefixes, while Internet Explorer and Android 4.2.0 browser does not support it. Chrome,Firefox和Safari支持通过CSS样式对svg <g>元素进行转换(不带前缀),而Internet Explorer和Android 4.2.0浏览器不支持。

I want to check if the browser supports this feature or not by JavaScript, for example like this: 我想检查浏览器是否通过JavaScript支持此功能,例如:

if ( svg_css_Transform_supported() ){
//do something;
}else{
//do nothing;
}

This is a JSFiddle tutorial running well on all supported browsers: 这是一个JSFiddle教程,可以在所有支持的浏览器上正常运行:

http://jsfiddle.net/samehobada/w95298kf/ http://jsfiddle.net/samehobada/w95298kf/

function svg_css_Transform_supported()
{
    var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
    return 'transform' in svg;
}

I searched a lot after this solution, and found another technique from modernizr threads (elementFromPoint). 在此解决方案之后,我进行了大量搜索,然后发现了来自modernizr线程(elementFromPoint)的另一种技术。 Since you don't need to check this every time, you can store it in cookie. 由于您不需要每次都检查,因此可以将其存储在cookie中。

I'm using jQuery and its cookie pluggin, but the concept stays the same if you don't : 我使用的是jQuery及其cookie插件,但是如果您不这样做,该概念将保持不变:

 // svg transform support test svgTransformSupport = function () { if (typeof $.cookie('svgtransform') === 'undefined') { var svg = $('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2 2" id="oksvgtransform"><path id="nosvgtransform" d="M0 0h1v1H0z"/></svg>'); $body.append(svg); // straight test if ('transform' in svg[0]) { $.cookie('svgtransform',true); console.log('straight css test svg transform ok'); return true; } // real test svg.css({ 'position':'fixed', 'width':'2px', 'height':'2px', 'left':'0px', 'top':'0px', 'z-index':'1000', }); $('#nosvgtransform').css({ 'transform':'translate(1px,1px)', }); var isSupport = (document.elementFromPoint(0, 0).id=="oksvgtransform"); svg.remove(); $.cookie('svgtransform',isSupport); if (isSupport) { console.log('element from point test svg transform ok'); } else { console.log('element from point test svg transform failed'); } return(isSupport); } else if ($.cookie('svgtransform')=="true") { console.log('cookie test svg transform ok'); return (true); } else { console.log('cookie test svg transform fail'); return (false); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM