Intersection observer not working as expected

Abdalla Abdalmunim

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. 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 !

this is the markup index.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 :

/* 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:

// 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);
})
Lakshya Thakur

You didn't add the check for entry.isIntersecting for the elements being observed. Also I switched from toggle to add the 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.

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Intersection Observer not working with siblings

WeakHashMap not working as expected in observer pattern

Intersection Observer API not working with hidden elements

Intersection observer not working on elements added through JS

Mobx Observer not working as expected when using FutureBuilder

Intersection Observer not working below 600px viewport (+GSAP)

intersection observer multiple animation

Intersection Observer is not trustable?

Animating with Intersection Observer

Intersection Observer API is not available

Intersection observer for jquery

single intersection observer for multiple entries

Intersection observer with child component in react

Mock for intersection observer in jest and typescript

Problem with Intersection Observer API and Bootstrap

Intersection Observer in Edge / Problem in the animation

Different callbacks for different targets with Intersection Observer API

How to lazy load images using intersection observer?

Intersection Observer fine tuning for different window sizes

Load all images when intersection observer is not supported

ReactJS — How to use intersection observer in a Class Component?

Intersection Observer using querySelectorAll throws a TypeError

Intersection Observer invoking function for 1 time

Intersection Observer cannot increase only the top rootMargin

How to dynamically change Intersection Observer's configuration

Intersection Observer API going into infinite rendering loop

Change style header/nav with Intersection Observer (IO)

Delay translate elements with Intersection Observer API

why does intersection observer keeps on running?