[英]Intersection observer not working as expected
I've got tow section
tags that I want them to appear when they get scrolled, and the class is-visible
gets added to the sections when they pass through the viewport.我有两个
section
标签,我希望它们在滚动时出现,并且 class is-visible
在它们通过视口时被添加到部分中。 the problem is that the class is-visible
gets added before the elements pass the viewport, and removed when they passed,, it's something wierd I don't what the mistake I did, I might be missing the obvious, any help I would be very gratefull !问题是 class
is-visible
在元素通过视口之前被添加,并在它们通过时被删除,这很奇怪我不知道我犯了什么错误,我可能会错过明显的任何帮助非常感谢!
this is the markup index.html :这是标记索引。html :
<section class="section scrollspy services animated-content" id="services">
<div class="container">
<h2 class="services-title show-onscroll">What We Do</h2>
<!-- animated content 1 -->
<section class="Analysis show-onscroll">
<h3 class="tab-title ">Media Analysis</h3>
</section>
<!-- animated content 2 -->
<section class="Monitoring show-onscroll">
<h3 class="tab-title ">Media Monitoring</h3>
</section>
</div>
</section>
style.css:样式.css:
/* Animated services */
.animated-content div.container>section {
border: 1em solid #fff;
border-bottom: 4em solid #fff;
border-radius: .25em;
box-shadow: 1em 1em 2em .25em rgba(0, 0, 0, .2);
margin: 2em auto;
opacity: 0;
transform: translateY(4em) rotateZ(-5deg);
transition: transform 4s .25s cubic-bezier(0, 1, .3, 1), opacity .3s .25s ease-out;
max-width: 600px;
width: 90%;
will-change: transform, opacity;
}
div.container>section.is-visible {
opacity: 1;
transform: rotateZ(-2deg);
}
.animated-content .services-title {
transform: translateY(4rem) scale(1.2);
opacity: 0;
}
.animated-content .services-title.is-visible {
animation: clear .9s forwards;
}
.animated-content section>h3 {
color: var(--heading-font-color);
font-size: 2rem;
}
.animated-content section>h5 {
color: var(--heading-font-color);
font-size: 1.5rem;
margin-bottom: 0;
}
.animated-content section>p {
margin: 0;
}
@keyframes clear {
to {
opacity: 1;
transform: none;
}
}
the index.js : index.js :
// Intersection observer
const callback = function (entries) {
entries.forEach(entry => {
entry.target.classList.toggle('is-visible');
});
}
const observer = new IntersectionObserver(callback);
const targets = document.querySelectorAll('.show-onscroll');
targets.forEach(target => {
observer.observe(target);
})
You didn't add the check for entry.isIntersecting
for the elements being observed.您没有为正在观察的元素添加对
entry.isIntersecting
的检查。 Also I switched from toggle
to add
the is-visible
class.我也从
toggle
到add
is-visible
class。 Currently I am not sure if you want to remove the is_visible
class when element is not visible but that can be done in the else
part of entry.isIntersecting
.目前,我不确定是否要在元素不可见时删除
is_visible
class 但可以在entry.isIntersecting
的else
部分中完成。
So for now as soon as our element get's visible, I stop observing it.所以现在只要我们的元素可见,我就停止观察它。
// Intersection observer const callback = function (entries) { entries.forEach(entry => { if(entry.isIntersecting) { entry.target.classList.add('is-visible'); observer.unobserve(entry.target) } }); } const observer = new IntersectionObserver(callback); const targets = document.querySelectorAll('.show-onscroll'); targets.forEach(target => { observer.observe(target); })
/* Animated services */.animated-content div.container>section { border: 1em solid #fff; border-bottom: 4em solid #fff; border-radius: .25em; box-shadow: 1em 1em 2em.25em rgba(0, 0, 0, .2); margin: 2em auto; opacity: 0; transform: translateY(4em) rotateZ(-5deg); transition: transform 4s.25s cubic-bezier(0, 1, .3, 1), opacity.3s.25s ease-out; max-width: 600px; width: 90%; will-change: transform, opacity; } div.container>section.is-visible { opacity: 1; transform: rotateZ(-2deg); }.animated-content.services-title { transform: translateY(4rem) scale(1.2); opacity: 0; }.animated-content.services-title.is-visible { animation: clear.9s forwards; }.animated-content section>h3 { color: var(--heading-font-color); font-size: 2rem; }.animated-content section>h5 { color: var(--heading-font-color); font-size: 1.5rem; margin-bottom: 0; }.animated-content section>p { margin: 0; } @keyframes clear { to { opacity: 1; transform: none; } }
<section class="section scrollspy services animated-content" id="services"> <div class="container"> <h2 class="services-title show-onscroll">What We Do</h2> <!-- animated content 1 --> <section class="Analysis show-onscroll"> <h3 class="tab-title ">Media Analysis</h3> </section> <!-- animated content 2 --> <section class="Monitoring show-onscroll"> <h3 class="tab-title ">Media Monitoring</h3> </section> </div> </section>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.