十字路口旁边有一个路口

As developing for the web has matured and JavaScript engines have become faster, one area remains a significant bottleneck - rendering. It's because of this that so many of the recent development efforts have been focused around rendering, with virtual DOM being one of the more popular examples. In 随着网络开发的日趋成熟和JavaScript引擎变得越来越快,一个领域仍然是显着的瓶颈-渲染。 因此,最近的许多开发工作都集中在渲染上,其中虚拟DOM是最受欢迎的示例之一。 在

Dojo 2, being aware of these new APIs and approaches has been a priority. But working with a new API has its challenges and the Dojo 2中 ,优先考虑这些新的API和方法。 但是使用新的API会遇到挑战,并且Intersection Observer API is no different. Intersection Observer API也不例外。

Intersection Observers have the goal of providing "a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport." This will allow sites to lazy-load images and other media, render and remove DOM on demand as we would need for a million-row grid, and provide infinite scrolling as we may see in a social network feed.

交集观察者的目标是提供“一种异步观察目标元素与祖先元素或顶级文档的视口相交的变化的方法”。 这将使站点能够延迟加载图像和其他媒体,按需渲染和删除DOM(就像我们需要一百万行网格一样),并提供无限滚动,就像我们在社交网络Feed中看到的那样。

But Intersection Observers also solve a bigger problem not immediately obvious to us as developers and outlined in the Web Incubator Community Group's Intersection Observer explanation document: displaying ads. The Interactive Advertising Bureau has a policy that ads must be 50% visible for more than a continuous second. With third-party advertising and page-impression scripts being notorious for contributing to page bloat, this API seems all the more important.

但是Intersection Observers还解决了一个更大的问题,这对我们作为开发人员来说不是立即显而易见的,并在Web孵化器社区小组的Intersection Observer解释文档中概述:显示广告。 互动广告局制定了一项政策 ,规定广告必须在50%可见度内持续超过一秒钟。 众所周知,第三方广告和页面展示脚本会导致页面膨胀,因此该API显得尤为重要。

Should we all immediately get to work integrating Intersection Observers into our projects? Unfortunately, there are a number of challenges, inconsistencies, and bugs that currently make it just out of reach and the leading polyfill implementation has a number of outstanding issues. But that does not mean the ability to use Intersection Observers is far off and we hope that by outlining the issues, creating tests, and submitting bug reports, viable use is only a few months away.

我们所有人都应该立即着手将交叉口观察员集成到我们的项目中吗? 不幸的是,目前有许多挑战,不一致和错误使它遥不可及,并且领先的polyfill实施存在许多悬而未决的问题。 但这并不意味着使用Intersection Observers的能力就遥遥无期,我们希望通过概述问题,创建测试和提交错误报告,可行的使用距离只有几个月。

这个怎么运作 ( How it Works )

Intersection Observers work in two parts: an observer instance attached to either a specific node or to the overall viewport and a request to this observer to monitor specific children within its descendants. When the observer is created, it is also provided with a callback that receives one or more intersection entries.
const observer = new IntersectionObserver((entries) = > {
entries.forEach(entry = > console.log(entry.target, entry. intersectionRatio));
});
observer.observe(node);

These entries are the heart of the API. Each has information outlining the intersection change and the node whose visibility is currently changing. Three properties are at the core of these entry objects, each providing a dimension of different information:

这些条目是API的核心。 每个节点都有概述交集变化和其可见性当前正在变化的节点的信息。 这些条目对象的核心是三个属性,每个属性提供一个不同信息的维度:

  • isIntersecting indicates whether the node assigned to the target property is visible within the observer's root

    isIntersecting指示分配给target属性的节点是否在观察者的根目录中可见

  • intersectionRatio is a number between 0 and 1 indicating the ratio of the target's view within the observer's root

    intersectionRatio比率是0到1之间的数字,指示目标视图在观察者根中的比例

  • intersectionRect is an object with numbers indicating the size with width and height, and the position with top, left, bottom, and right

    intersectionRect是一个对象,其数字表示宽度和高度,尺寸表示顶部,左侧,底部和右侧

Though the API is simple, its use can be complex and unique to each use case. Several examples are provided in the Web Incubator Community Group's Intersection Observer explanation document.

尽管该API很简单,但其用法可能很复杂,并且对于每个用例都是唯一的。 Web孵化器社区组的Intersection Observer解释文档中提供了几个示例。

问题:比率为0 ( Problem: A Ratio of 0 )

One of the easiest bugs to encounter is running into an intersection ratio of 0. It is a problem because it can happen both when a node is becoming visible and when a node is no longer visible. In the example below, when scrolling through the rows, you may notice a ratio of 0 appear occasionally. If not, scroll very slowly until the next row appears.

最容易遇到的错误之一是交叉比率为0。这是一个问题,因为它可能在节点变得可见时以及节点不再可见时发生。 在下面的示例中,滚动浏览各行时,您可能会发现偶尔会出现比率0。 如果不是,请非常缓慢地滚动直到出现下一行。

演示地址

This example is reading the intersectionRatio property of the IntersectionObserverEntry passed to the callback. It seems like a logical property to use to detect an intersection - after all, wouldn't an intersection ratio of 0 mean it's not visible? But if we have code that is only executed if this ratio is non-zero, it will never be run. Furthermore, if only a single node is being observed and by skipping the intersection ratio of 0, no other events will fire, and no content updates will be performed.

本示例正在读取传递给回调的IntersectionObserverEntryintersectionRatio属性。 似乎可以用来检测相交的逻辑属性-毕竟,相交比0表示不可见吗? 但是,如果我们有仅在该比率不为零时才执行的代码,它将永远不会运行。 此外,如果仅观察到单个节点并且跳过交叉比率0,则不会触发其他事件,也不会执行任何内容更新。

The solution to this is using the isIntersecting property which is only true if this node is, or is becoming, visible. Unfortunately, if this code was being written in TypeScript, this property, at the time of this writing, did not exist in the IntersectionObserverEntry interface, so it would be easy to miss.

解决方案是使用isIntersecting属性,该属性仅在该节点可见或变为可见时才为true。 不幸的是,如果此代码是用TypeScript编写的,那么在撰写本文时,此属性在IntersectionObserverEntry接口中不存在 ,因此很容易错过。

警告:巨子 ( Caution: Giant Child )

When creating a new Intersection Observer, a number of configuration options may be passed, including a number of thresholds that allow for an intersection entry and an associated event to be fired as the percentage of its visibility changes.

创建新的“交叉口观察者”时,可能会传递许多配置选项,包括多个阈值,这些阈值允许在其可见性百分比发生变化时触发交叉口条目和关联事件。

In the W3C specification, an intersection entry is created when "intersectionRatio is greater than the last entry in observer.thresholds" where this ratio is "intersectionArea divided by targetArea." When a node is larger than the root node observing it, this ratio will steadily increase until the child node fills it, at which point the value will never reach 1 but remain the overall ratio of their two heights.

在W3C规范中 ,当“ intersectionRatio大于observer.thresholds ”中的最后一个条目时,将创建一个交集条目,其中此比率为“ intersectionArea除以targetArea 。 当一个节点大于观察它的根节点时,该比率将稳定增加,直到子节点将其填满为止,此时该值永远不会达到1,而是保持其两个高度的总比率。

This can be confusing if we are expecting intersectionRatio to steadily increase between 0 and 1, which isn't a goal of the Intersection Observer API, and has no logical way of being calculated. But even if this behavior is well understood, it should be noted that events stop firing at all once that ratio no longer changes. Even though intersectionRect.top continues to change, and could be useful to our callback, the ratio itself is not changing.

如果我们期望intersectionRatio在0和1之间稳定增加,这可能会造成混淆,这不是Intersection Observer API的目标,并且没有逻辑上的计算方法。 但是,即使这种行为已被很好地理解,也应注意,一旦该比率不再改变,事件将完全停止触发。 即使intersectionRect.top继续更改,并且可能对我们的回调很有用,但是比率本身并未更改。

In this demo, console logs show intersection entries for 3 nodes - above, giant, and below - with a large number of thresholds indicating every 1% change in intersection ratio. Pay attention to when "giant" fills the parent view and stops emitting events.

在此演示中 ,控制台日志显示3个节点的交集条目-上方,巨型和下方-带有大量阈值,指示交集比率每变化1%。 请注意“巨型”何时填充父视图并停止发出事件。

演示地址

警告:事件重复或丢失 ( Caution: Duplicate or Missing Events )

As the specification becomes clearer and edge cases are documented, there are going to be differences between browsers and the polyfill that should be expected and managed. Reading the discussion in

this issue illustrates some of the areas in the specification that still need work, some areas where the specification was changed because of this discussion, and even explanations by browser developers as to why decisions were made the way they were.

本期中的讨论内容将说明规范中仍需要工作的某些领域,由于该讨论而更改了规范的某些领域,甚至是浏览器开发人员对为何按原样做出决策的解释。

In this example, we can open up the console to monitor the events. At the time of this writing, we could see Firefox occasionally emitting two entries as a node became visible. Although it's more of an edge-case, in the issue linked above, there are also situations where an event may not be emitted. Until these are corrected, ensure your implementation will not break, especially with duplicate events.

在此示例中 ,我们可以打开控制台来监视事件。 在撰写本文时,我们可以看到Firefox在节点可见时偶尔会发出两个条目。 尽管这更像是一种极端情况,但在上面链接的问题中,也有可能没有发出事件的情况。 在更正这些错误之前,请确保您的实现不会中断,尤其是发生重复事件时。

问题:Polyfill ( Problem: Polyfill )

At the time of this writing, the Intersection Observer polyfill incorrectly overwrites native implementations of

IntersectionObserver due to a non-global reference. Previous versions failed to apply the polyfill where the native implementation was incorrect, meaning a patched version should be used until there is a new release.

由于非全局引用 ,“交点观察器” polyfill错误地覆盖了IntersectionObserver本地实现。 以前的版本无法在本机实现不正确的地方应用polyfill,这意味着应该使用修补的版本,直到有新版本发布为止。

The polyfill currently fires only on document scroll, window resize, and DOM mutation with a throttled/debounced intersection calculation after 100ms. An issue has been opened to add animation and transition events to cover more event types. The W3C specification notes that native intersection detection "[requires] extraordinary developer effort despite their widespread use" and so it should be expected that 100% coverage is going to be difficult to achieve.

目前,polyfill仅在文档滚动,窗口调整大小和DOM突变时触发,并会在100毫秒后进行节流/去抖动交点计算。 已打开一个问题以添加动画和过渡事件以涵盖更多事件类型。 W3C规范指出,本机相交检测“尽管需要广泛使用,但仍需要开发人员付出巨大的努力”,因此,应该期望将很难实现100%的覆盖率。

Finally, there is a situation in which the polyfill will not report an intersection. Because it is entirely event-driven, calling .observe on a node already in the DOM does not calculate intersections. We have submitted an issue that recreates this situation.

最后,存在这种情况,即polyfill无法报告相交。 因为它完全是事件驱动的, .observe在DOM中已经存在的节点上调用.observe不会计算交集。 我们提交了一个问题 ,重现了这种情况。

注意:scrollTop ( Caution: scrollTop )

While this word of warning doesn't directly relate to intersection observers, it is likely to cause grief when using a scrolling inline element. Browsers have chosen different approaches to what happens when nodes are mutated within a scrolling inline element.

尽管此警告词与路口观察者没有直接关系,但使用滚动内联元素时可能会引起悲伤。 浏览器已经选择了不同的方法来处理滚动内联元素中的节点发生突变时发生的情况。

In Chrome, adding and removing nodes will automatically adjust the scroll position of the parent, through the scrollTop property. Other browsers - Safari, for example - do not perform this calculation. Because of this, you will need to work around this limitation by manually adjusting scrollTop based on size changes to nodes that appear before the first visible row.

在Chrome中,添加和删除节点将通过scrollTop属性自动调整父级的滚动位置。 其他浏览器-例如Safari-不会执行此计算。 因此,您将需要通过根据出现在第一个可见行之前的节点的大小更改来手动调整scrollTop来解决此限制。

预后:到达目的地 ( Prognosis: Getting There )

If it can be assumed that all users visiting a rich web application will be on the latest version of the leading browsers, there is enough active development and bug-squashing to assume we'll have a stable API in the near future.

如果可以假设所有访问富Web应用程序的用户都将使用最新版本的领先浏览器,则有足够的活跃开发和漏洞消除功能,可以假定我们在不久的将来将拥有稳定的API。

But because most projects cannot make this assumption, the polyfill will have to stand in when needed. While we also expect this code to improve, there are inherent limitations to what can be calculated without having access to the rendering pipeline and native event loop. Using straightforward CSS and knowing the supported events match your use case should result in usable intersection events.

但是由于大多数项目无法做出此假设,因此在需要时必须使用polyfill。 尽管我们也希望此代码能够得到改进,但是在无法访问渲染管道和本机事件循环的情况下,可计算的内容存在固有的局限性。 使用简单CSS并知道所支持的事件与您的用例匹配,将导致可用的相交事件。

了解更多 ( Learning More )

SitePen provides web application development & consulting to enterprise teams worldwide. Connect with SitePen today to expand your team's experience, expertise and ability to achieve more.

SitePen为全球企业团队提供Web应用程序开发和咨询。 立即与SitePen联系,以扩展您的团队的经验,专业知识和取得更多成就的能力。

翻译自: https://davidwalsh.name/intersection-observers

十字路口旁边有一个路口


http://www.taodudu.cc/news/show-2100393.html

相关文章:

  • 地址后面的sessionid怎么消除_富贵包的消除和改善头前倾,通过运动和减肥可以吗?...
  • 十字线阵---CBF,传统波束形成
  • OpenCV3历程(4)——寻找直线的十字交叉点
  • html追加消除,HTML/CSS:在中间清除浮动元素而不添加不需要的标签
  • 十字绣图下载_十字绣与编程有什么关系? 比你想象的更多
  • 佩戴十字架项链有什么特殊含义?
  • 蒙特卡洛方法的应用——解决“彩色砖块”问题
  • 关于十字翻转棋的解法研究
  • Java画十字_用Java绘制对角线
  • Day 1:矩阵归零消除序列和
  • 十字消除 - Cocos2d-x 2.0.1
  • python画十字_Python解决十字消除棋
  • Qt-十字消除小游戏
  • 【C / EasyX】十字消除游戏的实现方法
  • C++入门——实现十字消除游戏
  • DOSBOX下载
  • DOSBox下载安装
  • win10运行DOSBox配置Debug
  • 进行DosBox的下载与配置
  • dosbox编译c语言,DOSBOX的简单使用
  • dosbox运行C语言,[转载]dosbox的使用方法
  • 汇编语言之DOSBox的安装和使用
  • DOSBOX 安装与使用
  • dosbox运行C语言,DosBox的基本设置和安装
  • dosbox 实现程序编译
  • 使用Dosbox运行程序
  • DOSBox安装流程
  • DOSBox安装及使用详解
  • Dos 模拟器 DosBox 下载使用
  • Windows10环境中下载DOSBox并进行debug配置

十字路口旁边有一个路口_观察路口观察员相关推荐

  1. 按钮旁边加一个提示_地铁站的那些“红色按钮”,你知道是干啥用的吗?乱按可能被拘留...

    地铁紧急停车按钮 图片来自网络 位置:站台两侧墙壁上,靠近列车车头.车尾两侧. 外观:上锁的红色四方小盒子,按钮为红色,旁边写有"紧急停车按钮"等字样. 使用:紧急时刻击碎中间玻璃 ...

  2. 十字路口待转区什么用_开车经过十字路口,什么时候可以进入左转待转区呢?...

    对于左转弯待转区,很多人可能都没见过,毕竟这不像斑马线一样随处可见,不熟练路况和交通标志的人来说,碰到了难免不知道怎么办,到底什么时候可以进入,什么时候不能,这都是一个问题. 如果你问这个区域是干什么 ...

  3. 关于幂律分布的一个笔记_哈克_新浪博客

    关于幂律分布的一个笔记_哈克_新浪博客 关于幂律分布的一个笔记     (2011-03-02 18:12:27)     转载▼     标签:     幂律     二八法则     杂谈     ...

  4. 指令流水 一个时钟周期 出一个结果_以SM3算法为例,构建一个软硬协作算法加速器:性能分析与优化...

    衡量一款 ASIC 芯片可以从 PPA 三个角度进行. PPA 指的是: Power/Performance/Area,功耗 / 性能 / 面积. 衡量 FPGA 设计同样可以参照 PPA,但又有所不 ...

  5. Leetcode_1279_红绿灯路口_多线程

    用一个布尔型变量保存目前红绿灯状态. 如果目前的车能通过红绿灯,就让他通过. 如果不能,改变灯的状态. class TrafficLight {boolean isAGreen; public Tra ...

  6. php在文本框中输入一个年份_判断其生肖_并输出在文本框旁边.代码,PHP开发工程师面试真题之Web网页设计(附参考答案)_PHP教程...

    一.Form表单 真题1:简述POST和GET传输的最大容量分别是多少? 参考答案: GET方法传递数据,控制在1MB之内(因为URL的长度限制在1MB字符以内):POST方法传输数据没有大小的限制. ...

  7. 十字路口待转区什么用_左转待转区的几种违章 稍不注意你绝对就要中招

    我们司机朋友在路上的时候,最怕的应该就是被拍照了吧,毕竟一次违章就是几百块钱,再加上还要扣分,更是让人头疼,那么我们在路上行驶的时候有哪些经常被忽视的违章呢?今天我们就来和大家分享一个比较常见的违章, ...

  8. 十字路口待转区什么用_这个区域是干嘛用的?详解左转弯待转区规则

    某天有妹子在朋友圈里抱怨:"今天开车等绿灯左转,明明只有直行是绿灯,左转还是红灯,后面的车就老嘀嘀让我走--怎么走啊?让姐闯红灯呀?就一神经病!"妹子怒了,她不理解后面的车为何会在 ...

  9. 高德地图只显示一个省_浅谈当下各种导航软件:高德地图、百度地图、腾讯地图...

    浅谈当下各种导航软件:高德地图.百度地图.腾讯地图,之前出门找不到路只能靠问路,现在我们生活中出现了各种各样的导航软件,甚至让用户出现了选择困难症,不知道选择哪一款软件比较号,在这里小编就要给大家来分 ...

  10. 怎样快速画出一个正方体_人教版小学数学五年级下册 长方体和正方体的体积 教案、课件,公开课视频...

    长方体和正方体体积 [教学目标]1.通过讲授,引导学生找出规律,总结出体积的公式. 2.指导学生运用公式正确计算长方体.正方体的体积. 3.培养学生积极思考.探索新知的思维品质. [教学重点]长方体. ...

最新文章

  1. 如果只能通过IE写博客【Do we write blog just only with IE?】
  2. 【C/C++12】天气APP:不同数据建表入表,数据交换(exptables.cpp,ftpputfiles.cpp)
  3. IntelliJ IDEA for Mac 在MacOS模式下的注释快捷键(Comment Shortcut)
  4. 解决vscode格式化代码html属性换行问题; ctrl+s格式化去除分号,格式化自动单引号;解决js格式化换行问题;mac上的settings.json完整配置
  5. sqlserver java odbc_Java JDBC------------------ODBC(SQLServer)链接
  6. 零基础机器学习(1)- 我们为什么要学习Python?
  7. 程序人生001--点滴感悟-随笔01
  8. Atitit r2017 r5 doc list on home ntpc.docx 驱动器 D 中的卷是 p2soft 卷的序列号是 9AD0-D3C8 D:\ati\r2017 v4 r
  9. 表白网页在线制作-我要表白网-最浪漫的表白网页在线生成网站
  10. 无限享受百度文库,财富值无视
  11. edgewin10无法安装_win10内置Edge浏览器遇到“您未安装FLASH控件”如何解决
  12. matlab仿真中直流电压,直流升压变换器的MATLAB仿真.doc
  13. linux执行脚本中方法,Linux中执行shell脚本命令的4种方法总结
  14. Python编程基础的应用
  15. 项目风险管理__常见题
  16. 自定义Navigationbar,使用Catagory
  17. 阿里云服务器ECS windows server已开放端口但连不上的问题
  18. Python【3】:格式化输出
  19. 设计模式-创建型模式:原型模式PrototypeModel
  20. tif转成bmp matlab,【转 】将图像转化成avi格式电影(bmp2avi,jpg2avi,tiff2avi等) - [Matlab]...

热门文章

  1. 关于office/word/excel/powerpoint/ppt弹出“配置进度”的解决办法
  2. 【模拟器】win 10:iTools 模拟器 ( iOS 模拟器) 下载与安装
  3. Android电量优化全解析
  4. JAVA共享图片管理系统毕业设计 开题报告.
  5. android中TextView属性之autoText解析
  6. 鸿蒙2.0公测版支持机型,华为鸿蒙2.0露真容,公测版支持机型公布
  7. 逆向工具IDA下载网址
  8. Opencv3 core模块解析之convertTo
  9. CCS软件仿真 手把手教你 CCS 软件仿真 TMS320F2812
  10. Mathtype使用技巧