javascript document.queryselector()isn' t识别html的元素
我正在尝试将卷轴触发的动画添加到我的在线投资组合网站,在我的经验列表之间,我的简单div作为分离器:
<div class='divider'></div>
我正在尝试将入口动画添加到。
我的JS如下:
//Scroll triggered animations
const obs = new IntersectionObserver(entries => {
entries.foreach(entry => {
console.log(entry);
if(entry.isIntersecting) {
entry.target.classList.add('divider-animation');
}
});
});
console.log(document.querySelectorAll(".divider"));
const dividerList = document.querySelectorAll(".divider");
dividerList.foreach(divider => {
obs.observe(divider);
})
console.log()
带有QuerySelectorall的语句正在返回一个空节点,即使我的分隔线直接放置在HTML中,所以我不确定为什么我的QuerySelector不确定我认识他们。
我怀疑在元素满载之前正在执行JS,但是我没有成功使用几种不同的等待方法。
I am trying to add scroll-triggered animations to my online portfolio website, and between my experience listings I have simple divs just as separators:
<div class='divider'></div>
that I am trying to add entrance animations to.
My JS is as follows:
//Scroll triggered animations
const obs = new IntersectionObserver(entries => {
entries.foreach(entry => {
console.log(entry);
if(entry.isIntersecting) {
entry.target.classList.add('divider-animation');
}
});
});
console.log(document.querySelectorAll(".divider"));
const dividerList = document.querySelectorAll(".divider");
dividerList.foreach(divider => {
obs.observe(divider);
})
The console.log()
statement with the querySelectorAll is returning an empty NodeList even though my dividers are placed in the HTML directly, so I'm unsure why my querySelector isn't recognizing them.
I'm suspicious that the JS is executing before the the elements are fully loaded, but I haven't had success with several different wait methods for that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使将脚本放在底部,也要确保在加载DOM后其执行。
另外,这是 foreach ,而不是foreach。
Even if you are placing the script at the bottom make sure it gets executed after the DOM is loaded.
Also, it's forEach, not foreach.