简体   繁体   English

Intersection Observer API:观察视口的中心

[英]Intersection Observer API: Observe the center of the viewport

I am trying to observe the intersection at center of viewport, I have tried with passing negative values in rootMargin我试图观察视口中心的交点,我尝试在 rootMargin 中传递负值

rootMargin: '-45% 10% -45%'

But it doesn't seems to be working properly.但它似乎不能正常工作。

My requirement is to observe the intersection when my element is reaching the center of screen.我的要求是当我的元素到达屏幕中心时观察交叉点。

Intersection Observer rootMargin and how intersections are handled could become a bit confusing after a couple of tries, so I'll try to elaborate how could you make this happen for a specific scenario:交叉点观察器 rootMargin 和交叉点的处理方式在几次尝试后可能会变得有点混乱,所以我将尝试详细说明如何在特定情况下实现这一点:

  • Detect the intersection of an element when its top or bottom reaches the vertical center of the viewport当元素的顶部或底部到达视口的垂直中心时检测元素的交集

Intersecting element's top/bottom at the center of the viewport在视口中心相交元素的顶部/底部

const intersectionAtTopOrBottomElement = document.querySelector('.top-bottom-element');

const elementHasIntersected = (entries, o) => {...};
const ioConfiguration = {
  /**
   * This rootMargin creates a horizontal line vertically centered
   * that will help trigger an intersection at that the very point.
   */
  rootMargin: '-50% 0% -50% 0%',

  /**
   * This is the default so you could remove it.
   * I just wanted to leave it here to make it more explicit
   * as this threshold is the only one that works with the above
   * rootMargin
   */
  threshold: 0 // 
};

const observer = new IntersectionObserver(elementHasIntersected, ioConfiguration);
observer.observe(intersectionAtTopOrBottomElement);

By doing this the elementHasIntersected callback will get called as soon as the element's most top or most bottom edge intersects with the center of the viewport.通过这样做, elementHasIntersected回调将在元素的最顶部或最底部边缘与视口中心相交时被调用。

Fairly easy right?相当容易吧? Now, if you'd like to achieve a similar "center intersection" scenario such as:现在,如果您想实现类似的“中心交叉点”场景,例如:

  • Detect the intersection of an element when its vertical center reaches the vertical center of the viewport当元素的垂直中心到达视口的垂直中心时检测元素的交集

Then, this would need a different approach as the rootMargin: '-50% 0% -50% 0%' would never allow the intersection to be triggered at 50%–as the element would never be 50% visible.然后,这将需要一种不同的方法作为rootMargin: '-50% 0% -50% 0%'永远不会允许在 50% 处触发交集——因为元素永远不会有 50% 可见。 I'd be happy to brainstorm about this specific scenario if it's valuable to you, so let me know.如果它对您有价值,我很乐意就这个特定场景进行头脑风暴,所以请告诉我。

Here's an article I posted about the Intersection Observer API and The Intersection Observer Playground I put together where you can try different configurations (this version has some limits) and maybe you'll find the combination you need.这是我发布一篇关于 Intersection Observer APIIntersection Observer Playground 的文章,我将它们放在一起,您可以在其中尝试不同的配置(此版本有一些限制),也许您会找到所需的组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM