博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用JavaScript检测供应商前缀
阅读量:2512 次
发布时间:2019-05-11

本文共 2451 字,大约阅读时间需要 8 分钟。

Regardless of our position on vendor prefixes, we have to live with them and occasionally use them to make things work.  These prefixes can be used in two formats:  the CSS format (-moz-, as in ) and the JS format (navigator.).  The awesome  project has a clever bit of JavaScript magic that detects those prefixes in the browser environment -- let me show you how it works!

无论我们在供应商前缀上的立场如何,我们都必须与他们共处,并偶尔使用它们来使工作正常。 这些前缀可以两种格式可以使用:在CSS格式( -moz- ,如在 )和JS格式( navigator. )。 令人敬畏的项目具有一些巧妙JavaScript魔术,可以检测浏览器环境中的这些前缀-让我向您展示它的工作原理!

JavaScript (The JavaScript)

The first step is retrieving the HTML element's CSSStyleDeclaration:

第一步是检索HTML元素的CSSStyleDeclaration

var styles = window.getComputedStyle(document.documentElement, ''),

The next step is converting it to an Array object and searching for a known prefix type, settling on Opera if none is found:

下一步是将其转换为Array对象并搜索已知的前缀类型,如果未找到,则在Opera上进行设置:

pre = (Array.prototype.slice      .call(styles)      .join('')       .match(/-(moz|webkit|ms)-/) || (styles.OLink === '' && ['', 'o'])    )[1]

With the CSS prefix found, getting the JS-formatted prefix is simple:

找到CSS前缀后,获取JS格式的前缀很简单:

return {    dom: dom,    lowercase: pre,    css: '-' + pre + '-',    js: pre[0].toUpperCase() + pre.substr(1)  }

The returned object provides an object that looks something like:

返回的对象提供了一个类似于以下内容的对象:

Object {dom: "WebKit", lowercase: "webkit", css: "-webkit-", js: "Webkit"}

A complete representation of the vendor prefixing for the host browser. Here's the complete snippet:

主机浏览器的供应商前缀的完整表示。 这是完整的代码段:

var prefix = (function () {  var styles = window.getComputedStyle(document.documentElement, ''),    pre = (Array.prototype.slice      .call(styles)      .join('')       .match(/-(moz|webkit|ms)-/) || (styles.OLink === '' && ['', 'o'])    )[1],    dom = ('WebKit|Moz|MS|O').match(new RegExp('(' + pre + ')', 'i'))[1];  return {    dom: dom,    lowercase: pre,    css: '-' + pre + '-',    js: pre[0].toUpperCase() + pre.substr(1)  };})();

Grabbing the CSSStyleDeclaration from the HTML element is a clever move.  This method plays off of the fact that there will always be a vendor-prefixed property in the style declaration, which some may dislike, but is going to be effective for a long time to come.  What do you think of this method of vendor prefix detection?  Share your thoughts!

从HTML元素获取CSSStyleDeclaration是一个聪明的举动。 该方法是基于以下事实:样式声明中始终会存在一个供应商前缀的属性,有些人可能会不喜欢它,但是这种属性将在很长一段时间内有效。 您如何看待这种供应商前缀检测方法? 分享你的意见!

翻译自:

转载地址:http://nzpwd.baihongyu.com/

你可能感兴趣的文章
AngularJs学习笔记-慕课网AngularJS实战
查看>>
数据库三大范式
查看>>
工作总结之二:bug级别、优先级别、bug状态
查看>>
访问修饰符、封装、继承
查看>>
更换pip源到国内镜像,提升pip下载速度.
查看>>
POJ 2265 Bee Maja (找规律)
查看>>
Kendo MVVM 数据绑定(七) Invisible/Visible
查看>>
[zz]kvm环境使用libvirt创建虚拟机
查看>>
bzoj1059 [ZJOI2007]矩阵游戏
查看>>
插入返回ibatis 的selectKey 实现插入数据后获得id
查看>>
vim 程序编辑器
查看>>
LIS(单调队列优化 C++ 版)(施工ing)
查看>>
刚接触Vuex
查看>>
四种加载React数据的技术对比(Meteor 转)
查看>>
Airthmetic_Approching
查看>>
操作文本文件
查看>>
公司项目的几个问题
查看>>
解决win7下打开Excel2007,报“向程序发送命令时出现问题”的错误
查看>>
Velocity快速入门教程
查看>>
关于集合常见的问题
查看>>