美文网首页
实现类似autoprefixer工具函数

实现类似autoprefixer工具函数

作者: 8d2855a6c5d0 | 来源:发表于2018-02-15 00:02 被阅读0次

现在项目中处理css兼容前缀基本上都在webpack中集成了autoprefixer自动添加前缀,但在JS中操作样式只能手动添加webkit前缀,这样造成了代码的冗余,看起来也让人不舒服。

this.$refs.node.style['transform'] = `scale(1.5)`;
this.$refs.node.style['webkitTransform'] = `scale(1.5)`;

可以实现一个类似autoprefixer的工具函数自动检测浏览器兼容性添加前缀,代码如下

/* dom.js */
    let elementStyle = document.createElement('div').style

    let vendor = (() => {
        let tranformNames = {
            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)
    }

使用

import { prefixStyle }  from 'dom'
const  transform = prefixStyle('transform ');

this.$refs.node.style[transform] = `scale(1.5)`;

这样看起来就清爽了好多,也会根据不同浏览器自动添加不同的兼容前缀。

相关文章

  • 实现类似autoprefixer工具函数

    现在项目中处理css兼容前缀基本上都在webpack中集成了autoprefixer自动添加前缀,但在JS中操作样...

  • MySql实现分类功能

    实现方法和excel if 函数类似,上代码~

  • R中使用merge()函数合并数据

    R语言的merge函数可以实现类似SQL的有点类似 left join right join 或者类似union的...

  • day2

    一个小案列类似后天管理 实现增删利用bootstrap快速建站代码 重点findIndex函数some函数 实现查...

  • Autoprefixer-css神器

    处理CSS前缀问题的神器——AutoPrefixer。AutoPrefixer官网 Autoprefixer是一个...

  • day3

    实现一个reduce函数,作用和原生的reduce类似。

  • MongoDB查询实现 行级别的聚合函数

    title: MongoDB 实现行级别的聚合函数(类似于Oracle的开窗函数)tags: []notebook...

  • Golang标准库——bytes

    bytes bytes包实现了操作[]byte的常用函数。本包的函数和strings包的函数相当类似。 Const...

  • 节流函数的实现

    节流函数(throttle) 上一篇文章说了防抖函数的实现,其实着两个函数是类似的,都可以实现性能优化,那么如何抉...

  • 【剑指Offer 49】把字符串转换成整数

    题目:实现一个函数stringToInt,实现把字符串转换成整数这个功能,不能使用atoi或者其他类似的库函数。 ...

网友评论

      本文标题:实现类似autoprefixer工具函数

      本文链接:https://www.haomeiwen.com/subject/kvsytftx.html