1
0
mirror of https://github.com/locomotivemtl/locomotive-boilerplate.git synced 2026-01-15 00:55:08 +08:00

add getTranslate function in utils

This commit is contained in:
Quentin Hocdé
2019-07-04 10:52:04 -04:00
parent 26cccc7d92
commit 5da3bcd961

View File

@@ -3,3 +3,19 @@ export function transform(el, transformValue){
el.style.msTransform = transformValue;
el.style.transform = transformValue;
}
export function getTranslate(el){
const translate = {}
if(!window.getComputedStyle) return;
const style = getComputedStyle(el);
const transform = style.transform || style.webkitTransform || style.mozTransform;
let mat = transform.match(/^matrix3d\((.+)\)$/);
if(mat) return parseFloat(mat[1].split(', ')[13]);
mat = transform.match(/^matrix\((.+)\)$/);
translate.x = mat ? parseFloat(mat[1].split(', ')[4]) : 0;
translate.y = mat ? parseFloat(mat[1].split(', ')[5]) : 0;
return translate;
}