javascript - 使用 Intersection Observer 的不同元素的不同根边距和动画
问题描述
大家好。我有一个我正在开发的网站,并且想要完成不同的滚动动画,例如简单的不透明度和 y 移动动画以及宽度增加动画以及不仅基于视口的自定义根边距。
我完成了我想要的结果,但在我的 html 中留下了三个完整的脚本,如下所示,每个元素我想要拥有自己的根边距和动画。只需使用不同的变量名称等即可使其工作。那么,是否有更有效的方法来缩短代码?
Javascript:
const images = document.querySelectorAll('.tag');
const options = {
threshold: 0,
rootMargin: "0% 0% -77% 0%"
};
observer = new IntersectionObserver((images) => {
images.forEach(entry => {
if (entry.intersectionRatio > 0) {
entry.target.style.animation = `tag-anim 1.5s ${entry.target.dataset.delay} forwards cubic-bezier(0.65, 0, 0.35, 1)`;
}
})
}, options);
images.forEach(images => {
observer.observe(images)
});
解决方案
推荐阅读
- apache - 如何使这个 .htaccess 服务器友好并适用于 .php?
- python - 为什么在python中重新分配对象私有属性不会抱怨?
- mysql - 从 excel 宏连接到 mysql db 时出现问题
- css - 变量中的 Sass 增量类
- c++ - 函数不返回值,而在 C++ 中使用向量
- javascript - 在对象数组中找到 5 个最大的数并将其推送到另一个数组
- jenkins - 使用 Google Kubernetes Engine 配置 Jenkins slave
- ssl - 是否有任何 Web 服务器支持 RFC8879 TLS 证书压缩?
- sql-server - SQL Server 2016 Standard - 高性能(异步)镜像
- javascript - 导航栏在右侧显示一个空白区域