-
-
Notifications
You must be signed in to change notification settings - Fork 117
IntersectionObserver #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
不支持IOS是不是意味着不能做移动端了? |
有 polyfill |
写了个demo, 比如 我要在滚动到离图片的位置多出300距离做操作, 就得在图片相邻的地方插入一个元素,或者在优化下用一个元素,但是增加了计算量 ,absolute且物理位置隐藏, 然后才能原有的效果。 相比较于,requestIdleCallback 做调度也能做不少事情。 不过这个api在一些需求上 确实很好使。 |
摘录自IntersectionObserver’s Coming into View
从规范来看, |
每一个 我们可以看一下 polyfill 的实现:
如果执行的是紧急任务,不想异步执行,可以调用同步方法 在 Intersection Observers Explainer Doc 中有个显示广告的例子: function visibleTimerCallback(element, observer) {
delete element.visibleTimeout;
// Process any pending observations
processChanges(observer.takeRecords());
if ('isVisible' in element) {
delete element.isVisible;
logImpressionToServer();
observer.unobserve(element);
}
} 如果异步的回调先执行了,那么当我们调用同步的 综上:
|
今天在 github trending 看到一个项目:stratiformltd/react-loadable-visibility
react-loadable-visibility 包装了 react-loadable 和 loadable-components,并且使用了 参考 |
很好的 +111 |
简介
你可以通过绑定
scroll
事件或者用一个周期性的定时器,然后在回调函数中调用元素的getBoundingClientRect()
获取元素位置实现这个功能。但是,这种实现方式性能极差,因为每次调用getBoundingClientRect()
都会强制浏览器 重新计算整个页面的布局 ,可能给你的网站造成相当大的闪烁。IntersectionObserver
就是为此而生的,它可以检测一个元素是否可见,IntersectionObserver
能让你知道一个被观测的元素什么时候进入或离开浏览器的视口。兼容性
Polyfill
WICG 提供了一个 polyfill
✔
✔
6+
✔
7+
✔
4.4+
实用程度
★★★★
相关链接
The text was updated successfully, but these errors were encountered: