概述

今天在项目中用到了 getBoundingClientRect 和 requestAnimFrame ,查了下它们的polyfill,记录下来,供以后开发时参考,相信对其他人也有用。

getBoundingClientRect

getBoundingClientRect 的 polyfill如下所示:

getBoundingClientRect(element) {
const rect = element.getBoundingClientRect(); // whether the IE version is lower than 11
const isIE = navigator.userAgent.indexOf('MSIE') !== -1; // fix ie document bounding top always 0 bug
const rectTop = isIE && element.tagName === 'HTML'
? -element.scrollTop
: rect.top; return {
left: rect.left,
top: rectTop,
right: rect.right,
bottom: rect.bottom,
width: rect.right - rect.left,
height: rect.bottom - rectTop,
};
}

requestAnimFrame

requestAnimFrame 的 polyfill如下所示:

polyfillRAF() {
let requestAnimFrame = window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.MozRequestAnimationFrame
|| window.msRequestAnimationFrame
|| window.ORequestAnimationFrame; const getNow = Date.now || (() => +new Date()); let lastTime = getNow(); if (!requestAnimFrame) {
requestAnimFrame = (callback) => {
// How long did it take to render?
const deltaTime = getNow() - lastTime;
const delay = Math.max(0, 1000 / 60 - deltaTime); return window.setTimeout(() => {
lastTime = getNow();
callback();
}, delay);
};
} return requestAnimFrame;
}

值得注意的是,在移动端上requestAnimFrame可能会有性能问题,这个时候在移动端上建议不使用requestAnimFrame而使用setTimeout,代码如下:

polyfillRAF() {
let requestAnimFrame = window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.MozRequestAnimationFrame
|| window.msRequestAnimationFrame
|| window.ORequestAnimationFrame; const getNow = Date.now || (() => +new Date()); let lastTime = getNow(); // 判断移动端
if (_isMobile || !requestAnimFrame) {
requestAnimFrame = (callback) => {
// How long did it take to render?
const deltaTime = getNow() - lastTime;
const delay = Math.max(0, 1000 / 60 - deltaTime); return window.setTimeout(() => {
lastTime = getNow();
callback();
}, delay);
};
} return requestAnimFrame;
}

getBoundingClientRect 和 requestAnimFrame 的polyfill的更多相关文章

  1. polyfill for Function--源码

    /** * polyfill for Function */ // from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer ...

  2. 盒子 offsetLeft、offsetTop、offsetWidth、getBoundingClientRect等属性解释

    offsetLeft 获取的是忽略 margin 当前元素距离上一级父节点(有没有设置position,有的话依据父节点,没有的话依据页面最左端这时候不管滚动条移到哪) 当前元素向左的位置 记住它会将 ...

  3. JavaScript中getBoundingClientRect()方法详解

    获取浏览器滚动的高度: scrollTop=document.documentElement.scrollTop || document.body.scrollTop getBoundingClien ...

  4. js中getBoundingClientRect的作用及兼容方案

    js中getBoundingClientRect的作用及兼容方案 1.getBoundingClientRect的作用 getBoundingClientRect用于获取某个html元素相对于视窗的位 ...

  5. JavaScriptPolyfillShim 在JavaScript中Shim和Polyfill有什么区别?

    在JavaScript的世界里,有两个词经常被提到,那就是Shim和Polyfill,它们指的都是什么,又有什么区别?在本文中,将简短的给大家介绍他们之间的联系和区别.Shim一个shim就是一个库, ...

  6. 传说中的requestAnimFrame

    //让浏览器以10ms绘制 兼容写法                window.requestAnimFrame = (function() {                    return ...

  7. 利用getBoundingClientRect方法实现简洁的sticky组件

    补充于2016-03-20: 本文实现有不足,不完美的地方,请在了解本文相关内容后,移步阅读<sticky组件的改进实现>了解更佳的实现. sticky组件,通常应用于导航条或者工具栏,当 ...

  8. isArray polyfill

    Array.isArray在ie9+浏览器上已经支持,可以放心使用.在垃圾浏览器上,可以说使用如下polyfill(出自MDN) if(!Array.isArray){ Array.isArray = ...

  9. CSSOM之getboundingclientrect和getclientrects

    TextRectangle 对于文本对象,W3C提供了一个 TextRectangle 对象,这个对象是对文本区域的一个解释. 对于 i,span,em等display 是inline的标签,在书写文 ...

随机推荐

  1. vue - helloVue

    开始学习vue了 1.数据绑定:{{data}} 2.el属性(挂载对象): el:标签任意(例如:#app,.app,app) 3.data:{} :存放数据. <!DOCTYPE html& ...

  2. RGB颜色值转换成十六进制

    function transferRgbToStr(color) { if (typeof color !== 'string' && !(color instanceof Strin ...

  3. luffyapi项目 --短信认证的基本操作

    一.开通腾讯云短信 SDK 文档 :https://cloud.tencent.com/document/product/382/11672 1.官网注册实名账号:https://cloud.tenc ...

  4. kickstart一键装机部署

    1.第一步安装DHCP yum -y install dhcp 1.1配置修改文件 cat >> /etc/dhcp/dhcpd.conf <<END subnet 172.1 ...

  5. Redis在centos上面的安装

    一.安装redis 第一步:下载redis安装包 wget http://download.redis.io/releases/redis-4.0.6.tar.gz [root@iZwz991stxd ...

  6. 使用注解方式实现账户的CRUD

    1 需求和技术要求 1.1 需求 实现账户的CRUD. 1.2 技术要求 使用Spring的IOC实现对象的管理. 使用QueryRunner作为持久层的解决方案. 使用C3p0作为数据源. 2 环境 ...

  7. java 各版本新特性

    Java 5,6,7,8,9,10,11新特性吐血总结 lkd_whh关注赞赏支持 12018.04.01 14:09:15字数 1,948阅读 10,615 作者:拔剑少年 简书地址:https:/ ...

  8. R语言-六大数据结构

    R语言有六种基本的数据结构(或者说数据类型吧).根据数据的维度和同质/异质可分为5种数据类型,最后再介绍一种特殊的类型“因子”.   同质 异质 1维 原子向量 列表 2维 矩阵 数据框 n维 数组 ...

  9. HTML5测试(一)

    HTML5测试一 1. 问题:HTML5 之前的 HTML 版本是什么? A.HTML 4.01 B.HTML 4 C.HTML 4.1 D.HTML 4.9 答案:A HTML5 是 HTML 最新 ...

  10. python导入包失败ModuleNotFoundError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package

    最近在看 python,在使用matplotlib进行绘图时,提示:ModuleNotFoundError: No module named 'matplotlib.pyplot'; 'matplot ...