概述

今天在项目中用到了 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. 4、MySQL 申明变量给查询数据编号

    摘自: https://www.cnblogs.com/qixuejia/archive/2010/12/21/1913203.html https://blog.csdn.net/arbben/ar ...

  2. host.conf - 解析配置文件

    DESCRIPTION (描述) 文件 /etc/host.conf 包含了为解析库声明的配置信息. 它应该每行含一个配置关键字, 其后跟着合适的配置信息. 系统识别的关键字有: order, tri ...

  3. Java中Collection、Map常用实现类研究分析

    接口/实现类 描述 key是否可为null 为null是否报错 key是否重复 key重复是否报错 key是否和添加一致 是否线程安全 List 一组元素的集合 ArrayList 基于数组存储,读取 ...

  4. Linux20期学习笔记 Day4

    环境变量.常用系统变量:vim编辑器使用方法及实验:shell脚本两个层次 内置参数及相关实验

  5. CentOS7+ 普通用户使用密钥登陆服务器(同时禁用root登陆)

    创建普通用户: # useradd user01 # tail -n2 /etc/passwd chrony:x:998:996::/var/lib/chrony:/sbin/nologin user ...

  6. jmeter之Ramp-up Period(in seconds)

    [1]决定多长时间启动所有线程.如果使用10个线程,ramp-up period是100秒,那么JMeter用100秒使所有10个线程启动并运行.每个线程会在上一个线程启动后10秒(100/10)启动 ...

  7. json格式字符串转字典

    //json格式字符串转字典+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {        if (jsonStr ...

  8. Luogu P3886 [JLOI2009]神秘的生物 最小表示法,轮廓线DP,插头DP,动态规划

    亲手写掉的第一道最小表示法!哈哈哈太开心啦~ 不同于以往的几个插头\(dp\),这个题目的轮廓线是周围的一圈\(n\)个格子.而其所谓"插头"也变成了相邻格子的所属连通分量编号,并 ...

  9. MYSQL<五>

    -- ########## 01.LIMIT的使用和分页 ########## INSERT INTO studentinfo VALUES(NULL, '刘备', '男', 35), (NULL, ...

  10. 在centos7.5使用DockerFile构建镜像时报错“Error parsing reference: "microsoft/dotnet:2.2-aspnetcore-runtime AS base" is not a valid repository/tag: invalid reference format”

    运行dockerfile时报出的错误 FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base Error parsing reference: &qu ...