js 浏览器兼容css中webkit、Moz、O、ms...写法封装(es6语法)
程序员文章站
2022-07-02 21:27:32
//调用 const transform = prefixStyle('transform') const backdrop = prefixStyle('backdrop-filter') div.style[transform] = `translate3d(0,0,0)` 等同于 div.st ......
/** *浏览器兼容写法封装 */ let elementstyle = document.createelement('div').style let vendor = (() => { let transformnames = { webkit: 'webkittransform', moz: 'moztransform', o: 'otransform', ms: 'mstransform', standard: 'transform' } for (let key in transformnames) { if (elementstyle[transformnames[key]] !== undefined) { return key } } return false })() export function prefixstyle(style) { if (vendor === false) { return false } if (vendor === 'standard') { return style } return vendor + style.charat(0).touppercase() + style.substr(1) }
//调用
const transform = prefixstyle('transform')
const backdrop = prefixstyle('backdrop-filter')
div.style[transform] = `translate3d(0,0,0)` 等同于 div.style['transform'] = `translate3d(0,0,0)`
div.style[backdrop] = `blur(4px)`等同于 div.style['backdrop'] = `blur(4px)`
上一篇: .NET 基础知识
下一篇: MYSQL存储过程,函数,光标