Skip to content

Instantly share code, notes, and snippets.

@iamso
Created February 27, 2017 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamso/0a57bfacea668595a0d8966309b5333d to your computer and use it in GitHub Desktop.
Save iamso/0a57bfacea668595a0d8966309b5333d to your computer and use it in GitHub Desktop.
Check support for CSS transform on SVGs
const supportsCSSTransformsOnSVG = (() => {
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('viewBox', '0 0 2 2');
Object.assign(svg.style, {
position: 'fixed',
top: 0,
left: 0,
width: '2px',
height: '2px',
zIndex: 2147483647,
});
svg.innerHTML = '<rect width="1" height="1" style="transform: translate(1px, 1px)"/>';
document.body.appendChild(svg);
const result = document.elementFromPoint(1, 1) !== svg;
svg.parentNode.removeChild(svg);
return result;
})();
// use like this
if (supportsCSSTransformsOnSVG) {
el.style.transform = `translate(${dx}px, ${dy}px)`;
} else {
el.setAttribute('transform', `translate(${dx} ${dy})`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment