javascript - Intersection Observer 在 600px 视口 (+GSAP) 以下不工作
问题描述
我的内容动画有问题。一切正常,但是当我制作响应式网站并使视口宽度低于 600 像素时,我的交叉点观察器无法正常工作。什么都没发生。
当我滚动我的网站时,我将 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);
});
解决方案
你有rootMargin: "-300px"
. 这意味着相交不会开始,直到您在任何一侧达到 300 像素。当视口小于 600px 时,没有更多元素可以相交,因此它不起作用。
有关更多信息,请参阅交叉点观察者文档。
推荐阅读
- tensorflow - 使用 tf.keras 模型训练时 GPU 随机冻结
- amazon-web-services - 使用客户端应用程序更新非 ssl 中的 Amazon RDS SSL/TLS 证书
- javascript - 哪些运行时平台和版本实现了 ES6 模块和导入导出语法?
- python - DQN+CPLEX:随着迭代的进行,我的程序变得越来越慢
- c - 关于数组的最终分配问题
- c# - 求解数学最后 str + 1 返回 50
- javascript - 我得到了网络响应,但 CORS 不允许它进入
- php - 我想使用 php 使用简单的 html dom 来抓取输入值 readonly 值,我一直得到一个空白返回
- react-native - 反应本机 - 在 RTL 设备中使特定组件不是 RTL
- python - Python 中的 Lambda 递归和 AND/OR