浏览器兼容性:JavaScript polyfill

由于 JavaScript API 有自己的规范,因此并非所有浏览器都同时支持特定规范。因此,我们必须提供 API 的补丁版本,以确保它仍然可以在不原生支持它的浏览器上运行。这种补丁称为 polyfill。

比如为 Safari 15.4 之前提供 Array 的at()方法的补丁:

Array.prototype.at =
    Array.prototype.at ||
    function (index) {
        // The implementation ...
    };

又比如在Safari 15.4 之前 structedClone不可用,也需要打上补丁:

typeof window.structuredClone !== 'function' &&
    window.structuredClone = function (value) {
        // ...
    };

出于为 CSS 属性添加前缀的相同原因,浏览器也在实验阶段为 JavaScript API 添加前缀。下表列出了最流行的浏览器的前缀: 

前缀浏览器
mozfirefox
ms使用 Chromium 引擎之前的 Internet Explorer 和 Microsoft Edge
o使用 WebKit 引擎之前的 Opera 旧版本
webkitGoogle Chrome、Safari、更新版本的 Opera 和 Edge 以及几乎所有的 iOS 浏览器

比如,针对requestAnimationFrame函数:

window.requestAnimationFrame =
    window.requestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame;

全屏 API 则是更复杂的场景,支持全屏模式必须涵盖以下功能:

标准API描述
fullscreenEnabled检查是否启用了全屏模式
requestFullscreen请求浏览器进入全屏模式
fullscreenElement返回全屏元素
fullscreenchange全屏模式发生时触发该事件
exitFullscreen退出全屏模式

首先需要一个支持该接口的包装器: 

enum Api {
    ExitFullScreen,
    FullScreenChange,
    FullScreenElement,
    FullScreenEnabled,
    RequestFullScreen,
}

type ApiMap = {
    [key in keyof typeof Api]: string;
};

针对不同浏览器,定义每个API的包装器:

const defaultVendor: ApiMap = {
    ExitFullScreen: 'exitFullscreen',
    FullScreenChange: 'fullscreenchange',
    FullScreenElement: 'fullscreenElement',
    FullScreenEnabled: 'fullscreenEnabled',
    RequestFullScreen: 'requestFullscreen',
};

const webkitVendor: ApiMap = {
    ExitFullScreen: 'webkitExitFullscreen',
    FullScreenChange: 'webkitfullscreenchange',
    FullScreenElement: 'webkitFullscreenElement',
    FullScreenEnabled: 'webkitFullscreenEnabled',
    RequestFullScreen: 'webkitRequestFullscreen',
};

const mozVendor: ApiMap = {
    ExitFullScreen: 'mozCancelFullScreen',
    FullScreenChange: 'mozfullscreenchange',
    FullScreenElement: 'mozFullScreenElement',
    FullScreenEnabled: 'mozFullScreenEnabled',
    RequestFullScreen: 'mozRequestFullScreen',
};

const msVendor: ApiMap = {
    ExitFullScreen: 'msExitFullscreen',
    FullScreenChange: 'msFullscreenChange',
    FullScreenElement: 'msFullscreenElement',
    FullScreenEnabled: 'msFullscreenEnabled',
    RequestFullScreen: 'msRequestFullscreen',
};

最后就可以得到当前浏览器支持的API集,直接简单地调用给定元素的全屏AP即可:

const vendor: ApiMap =
    (Api.FullScreenEnabled in document && defaultVendor) ||
    (webkitVendor.FullScreenEnabled in document && webkitVendor) ||
    (msVendor.FullScreenEnabled in document && msVendor) ||
    defaultVendor;

element[vendor.RequestFullScreen]();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛定谔的猫96

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值