简体   繁体   English

Intersection Observer API 不适用于变换:translateX()

[英]Intersection Observer API does not work with transform: translateX()

I have the Intersection Observer API working on my page with a few transitions from an example I found.我有Intersection Observer API在我的页面上工作,其中有一些来自我找到的示例的转换。

See here working scale-in transition: https://jsfiddle.net/wm8f4Lks/请参阅此处scale-in转换: https://jsfiddle.net/wm8f4Lks/

However, when I try to use the fade-from-left transition which transitions from translateX(-2000px) to translateX(0px) - nothing happens.但是,当我尝试使用从translateX(-2000px)过渡到translateX(0px) fade-from-left过渡时 - 没有任何反应。 The javascript does not even trigger the assignment of the class is-inViewport to the element which is confusing - I am not understanding how a css command can block a javascript command? javascript 甚至不会触发将 class is-inViewport分配给令人困惑的元素 - 我不明白 css2F5C6AE2A266AZ2F2E2919DEE700 的命令如何阻止 ZDE9B9BFFEE70DCE7 命令?

It is happening in the jsfiddle so I assume it is not just me, it's a thing, but I want to know how to get around this and apply this transition?它发生在 jsfiddle 中,所以我认为这不仅仅是我,而是一件事,但我想知道如何解决这个问题并应用这个转换?

Your problem is actually that the IntersectionObserver also does work with transform .您的问题实际上是 IntersectionObserver 也可以与transform一起使用。
The translate(-2000px, 0) moves makes your element 2000px on the left, its sizes is set to 100% (default), which corresponds to the size of the viewport - 16px (from the body's margins). translate(-2000px, 0)移动使您的元素在左侧 2000px,其大小设置为100% (默认),对应于视口的大小 - 16px(从身体的边距)。 If your viewport is smaller than 2000px, this means that your element is completely off screen.如果您的视口小于 2000 像素,这意味着您的元素完全不在屏幕上。 For instance if the viewport is 1024px, the element's width will be 1008px (1024 - 16).例如,如果视口为 1024px,则元素的宽度将为 1008px (1024 - 16)。 With the transform applied, its left coordinate will be -1992px (8 - 2000) and its right coordinate will be -984px (-1992 + 1008).应用变换后,其左坐标将为 -1992px (8 - 2000),其右坐标将为 -984px (-1992 + 1008)。 This means that it's not inside the current viewport, but off by 984px, on the left.这意味着它不在当前视口内,而是在左侧偏离 984 像素。

If you had your viewport larger than 2000px, then it would have worked.如果您的视口大于 2000 像素,那么它会起作用。

Note that you may want to revisit the value of the translation here, since using an hard-coded absolute value like that to affect an element that has a relative size is very often a bad idea.请注意,您可能希望在此处重新访问转换的值,因为使用像这样的硬编码绝对值来影响具有相对大小的元素通常是一个坏主意。

Unfortunately, pseudo elements don't trigger ObservationObservers, so we will need an actual element to act as a trigger: We'll keep that trigger in the viewport and place right above the one we want to animate.不幸的是,伪元素不会触发 ObservationObservers,所以我们需要一个实际的元素来充当触发器:我们将该触发器保留在视口中,并放置在我们想要动画的那个上面。 This way the observer will kick happily, and our element will be animatable however we want.这样观察者会开心地踢球,我们的元素将可以随心所欲地设置动画。

 const inViewport = (entries, observer) => { entries.forEach(entry => { entry.target.nextElementSibling.classList.toggle("is-inViewport", entry.isIntersecting); }); }; const Obs = new IntersectionObserver(inViewport); const obsOptions = {}; //See: https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Intersection_observer_options // Attach observer to every [data-inviewport] element: const ELs_inViewport = document.querySelectorAll('.animate-trigger'); ELs_inViewport.forEach(EL => { Obs.observe(EL, obsOptions); });
 .animate.scale-in { transition: 2s; transform: scale(0.1); }.animate.scale-in.is-inViewport { transform: scale(1); }.animate.fade-rotate { transition: 2s; opacity: 0; }.animate.fade-rotate.is-inViewport { transform: rotate(180deg); opacity: 1; }.animate.fade-from-left { transition: 2s; transform: translateX(-150%); /* Better not rely on hard-coded values */ }.animate.fade-from-left.is-inViewport { transform: translateX(0px); opacity: 1; width: 100%; }
 <div style="height: 200vh;"></div> <div class="animate-trigger" aria-hidden="true"></div> <!-- a new element --> <h2 class="animate fade-from-left">Heading</h2>

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

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