gpt4 book ai didi

javascript - 在带有溢出的 div 中使用 AOS(滚动动画)

转载 作者:行者123 更新时间:2023-11-28 00:19:41 24 4
gpt4 key购买 nike

当我向下滚动到它们时,我真的很想在屏幕上显示一些动画,就像它们在这里做的那样...

https://michalsnik.github.io/aos/

问题是我的网站实际上嵌套在一个名为“main-content”的 div 中,侧边栏和顶部栏有单独的 div。

它会出现在this question and answer在嵌套滚动条中不可能实现我想要的,但这是 3 年前的事了,我只是想知道是否有使用 aos 或 wow.js 或类似东西的潜在解决方法。

我已经尝试了几次但没有成功,但我觉得一定有办法实现这一目标。

提前致谢!

最佳答案

IntersectionObserver使这个 child 的行为很容易使用普通 JavaScript 自己实现。这是一个fairly new API但是有一个polyfill .

如果您想跟踪自己的滚动容器而不是文档,您可以将 root 设置为其他内容。

// Find the item we want to animate on scroll
var target = document.querySelector('#target');
var targetActiveClass = 'target-is-active';

// Call this function when it enters/leaves the viewport
var callback = function(entries, observer) {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add(targetActiveClass);
} else {
entry.target.classList.remove(targetActiveClass);
}
});
};

// Create our observer
var observer = new IntersectionObserver(callback, {threshold: 0});
observer.observe(target);
/* Our target, hidden by default */

#target {
align-items: center;
background-color: #000;
color: #fff;
display: flex;
justify-content: center;
height: 100px;
margin-bottom: 150vh;
margin-top: 150vh;
opacity: 0;
transform: translateX(-100%);
transition: opacity .25s ease-in-out,
transform .25s ease-in-out;
width: 200px;
}

/* Apply this class when #target enters/leaves the viewport */

#target.target-is-active {
opacity: 1;
transform: none;
}
<p>Scroll!</p>
<div id="target">Howdy!</div>

关于javascript - 在带有溢出的 div 中使用 AOS(滚动动画),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54935198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com