Intersection Observer在600px视口(+ GSAP)以下无法正常工作

问题描述 投票:1回答:1

我的内容动画有问题。一切正常,但是当我制作响应式网站并使视口宽度小于600px时,我的路口Observer无法正常工作。什么都没发生。

当我滚动网站时,我将console.log添加到IO,但控制台中没有任何显示。当宽度大于600像素时滚动时,我可以在控制台中看到IO。

也许有人知道该问题的解决方案?

我的代码:

//global
const slideDuration = 1;

// scroll animation
let target = document.querySelectorAll("section");
const options = {
 root: null,
 threshold: 0,
 rootMargin: "-300px"
};

const contentAnimation = (left, right) => {
  gsap
    .timeline()
    .from(right, { opacity: 0, duration: slideDuration, x: 400 })
    .from(left, { opacity: 0, duration: slideDuration, x: -400 }, "-=0.5");
};

 const io = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
  //show content
  const getElement = entry.target.className;
  const contentElement = document.querySelector(`.${getElement}__content`);
  contentElement.style.opacity = "1";

  //animation
  const right = document.querySelectorAll(`.${entry.target.className}-right`);
  const left = document.querySelectorAll(`.${entry.target.className}-left`);

  contentAnimation(left, right);
  io.unobserve(entry.target);
}
  });
}, options);

target.forEach(section => {
  io.observe(section);
});
javascript gsap intersection-observer
1个回答
0
投票

您有rootMargin: "-300px"。这意味着相交只有在您两边都达到300像素时才开始。当视口小于600像素时,没有更多的元素可以相交,因此它不起作用。

请参阅the intersection observer docs了解更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.