我有一些 Javascript 可以检测浏览器并根据浏览器对元素应用转换。 Webkit 的版本在 Chrome 上运行良好,但 Firefox 的版本则不行。有人可以告诉我下面的代码是否正确:
if(typeof navigator.vendor.toLowerCase().indexOf('chrome')!=-1){
document.getElementById('jj_preview7').style.WebkitTransform = 'scale(' + jj_input23 + ') ' + 'rotate(' + jj_input24 + 'deg)' + 'translate(' + jj_input25 + 'px, ' + jj_input26 + 'px)' + 'skew(' + jj_input27 + 'deg, ' + jj_input28 + 'deg)';
}
if(typeof navigator.vendor.toLowerCase().indexOf('firefox')!=-1){
document.getElementById('jj_preview7').style.MozTransform = 'scale(' + jj_input23 + ') ' + 'rotate(' + jj_input24 + 'deg)' + 'translate(' + jj_input25 + 'px, ' + jj_input26 + 'px)' + 'skew(' + jj_input27 + 'deg, ' + jj_input28 + 'deg)';
}
提前致谢
最佳答案
// Test element we apply both kinds of transforms to:
var testEl = document.createElement('div');
testEl.style.MozTransform = 'translate(100px) rotate(20deg)';
testEl.style.webkitTransform = 'translate(100px) rotate(20deg)';
var styleAttrLowercase = testEl.getAttribute('style').toLowerCase();
// when we check for existence of it in the style attribute;
// only valid ones will be there.
var hasMozTransform = styleAttrLowercase.indexOf('moz') !== -1;
var hasWebkitTransform = styleAttrLowercase.indexOf('webkit') !== -1;
现在您可以这样做:
var transformParts = [];
if (jj_input23 !== '') {
transformParts.push('scale(' + jj_input23 + ')');
}
if (jj_input23 !== '') {
transformParts.push('rotate(' + jj_input24 + 'deg)');
}
if (jj_input25 !== '' && jj_input26 !== '') {
transformParts.push('translate(' + jj_input25 + 'px, ' + jj_input26 + 'px)');
}
if (jj_input27 !== '' && jj_input28 !== '') {
transformParts.push('skewX(' + jj_input27 + 'deg) skewY(' + jj_input28 + 'deg)');
}
var transformTxt = transformParts.join(' ');
if (hasWebkitTransform) {
document.getElementById('jj_preview7').style.WebkitTransform = transformTxt;
}
if (hasMozTransform) {
document.getElementById('jj_preview7').style.MozTransform = transformTxt;
}
关于javascript - Webkit 和 Moz Transform,取决于浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10962511/