Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Observer script fails after second entry - any guesses

vonWenzel

New Coder
I’m trying to use an observer script to show / hide divs as others enter viewport. Below is the script I’m using. It works for the first two items, then fails when the third scrolls into view. Any idea why would be appreciated!

JavaScript:
<script type="text/javascript">

var observer = new IntersectionObserver(function(entries) {
    if(entries[0].isIntersecting === true){
        // on screen
    document.getElementById('feel-1').classList.add('is-visible');
    document.getElementById('feel-1').classList.remove('is-hidden');
  } else {
    // not on screen
    document.getElementById('feel-1').classList.add('is-hidden');
  document.getElementById('feel-1').classList.remove('is-visible');
  }
          if(entries[1].isIntersecting === true){
     document.getElementById('feel-2').classList.add('is-visible');
    document.getElementById('feel-2').classList.remove('is-hidden');
  } else {
    //  not on screen
    document.getElementById('feel-2').classList.add('is-hidden');
  document.getElementById('feel-2').classList.remove('is-visible');
  }
    if(entries[2].isIntersecting === true){
      document.getElementById('feel-3').classList.add('is-visible');
    document.getElementById('feel-3').classList.remove('is-hidden');
  } else {
    //  not on screen
    document.getElementById('feel-3').classList.add('is-hidden');
  document.getElementById('feel-3').classList.remove('is-visible');
  }{ threshold: [1] }
    
    
    
  });
observer.observe(document.querySelector("#middle-1"));
observer.observe(document.querySelector("#middle-2"));
observer.observe(document.querySelector("#middle-3"));


</script>
 
Sure, here's the html, or the relevant bits anyway.
HTML:
<div class="start_scroll_feel_more sticky-container-holder">
<div class="box-shadow-container-holder feel-more">
<div class="box-shadow-mid-page-titles absolute-position-titles feel-more">
<div id="feel-1" class="feel-more-word feel-more-word-1"><h2>We Create Images That Help You Feel More Love</h2></div>
<div id="feel-2" class="feel-more-word feel-more-word-2"><h2>We Create Images That Help You Feel More Confident</h2></div>
<div id="feel-3" class="feel-more-word feel-more-word-3"><h2>Change all the words</h2></div>
</div>
</div>
<div class="gb-container gb-container-b774367b"><div class="gb-inside-container"></div></div>

<div id="middle-1" class="middle-locator"></div><div class="section-background-photo scroll-background-section">
<div class="image-border-top"></div>
<picture>
<img height="367" width="550" class="section_background_image" src="https://focusphotography.com/wp-content/uploads/2022/03/Liz-Bert-136-A.webp" style="max-width: 100vw;min-width: 100vw; " alt="alt text for section 1">
</picture>
<div class="image-border-bottom"></div></div>


<div id="middle-2" class="middle-locator"></div><div class="section-background-photo scroll-background-section">
<div class="image-border-top"></div>
<picture><img height="367" width="550" class="section_background_image" src="https://focusphotography.com/wp-content/uploads/2022/03/KellyJosh-395.webp" style="max-width: 100vw;min-width: 100vw; " alt="alt text for section 2">
</picture>
<div class="image-border-bottom"></div></div>


<div id="middle-3" class="middle-locator"></div><div class="section-background-photo scroll-background-section">
<div class="image-border-top"></div>
<picture>
<img height="367" width="550" class="section_background_image" src="https://focusphotography.com/wp-content/uploads/2022/03/KatieJanWed-1969.webp" style="max-width: 100vw;min-width: 100vw; " alt="alt text for section 3">
</picture>
<div class="image-border-bottom"></div></div>
 
Dear, whats API you are using, did in this file IntersectionObserver you add the exports to make easy to use with another classes


 
Back
Top Bottom