title: How to get offsetTop of element in regards to the document and not its parent?
body: <p>I had trouble wording my question, but here's the detail ! I've been struggling with how to get my navigation bullets (spans) to be highlighted when the current element is in viewport. I've initially tried using the same logic as a sidemenu tutorial I previously used elsewhere (by Web Cifar on youtube), but it doesn't quite work here.
  If I console(log) the .bullet-slider class, they correctly show up. What the problem is, from what I gather, is the offsetTop of my const bulletSlide can't be calculated because the parentElement is another div and not the document (is this right?). I've looked into using an Intersection Observer instead, but being new to javascript I'm struggling in how I could implement it into my existing structure.</p>
  <p>Any help and/or tips would be greatly appreciated ! Thank you !</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>/**
  * By Alvaro Trigo - horizontal scroll  
  */(function(){
      init();
  
      var g_containerInViewport;
      function init(){
          setStickyContainersSize();
          bindEvents();
      }
  
      function bindEvents(){
          window.addEventListener("wheel", wheelHandler);        
      }
  
      function setStickyContainersSize(){
          document.querySelectorAll('.sticky-container').forEach(function(container){
              const stikyContainerHeight = container.querySelector('.main').scrollWidth;
              container.setAttribute('style', 'height: ' + stikyContainerHeight + 'px');
          });
      }
  
      function isElementInViewport (el) {
          const rect = el.getBoundingClientRect();
          return rect.top &lt;= 0 &amp;&amp; rect.bottom &gt; document.documentElement.clientHeight;
      }
  
      function wheelHandler(evt){
          
          const containerInViewPort = Array.from(document.querySelectorAll('.sticky-container')).filter(function(container){
              return isElementInViewport(container);
          })[0];
  
          if(!containerInViewPort){
              return;
          }
  
          var isPlaceHolderBelowTop = containerInViewPort.offsetTop &lt; document.documentElement.scrollTop;
          var isPlaceHolderBelowBottom = containerInViewPort.offsetTop + containerInViewPort.offsetHeight &gt; document.documentElement.scrollTop;
          let g_canScrollHorizontally = isPlaceHolderBelowTop &amp;&amp; isPlaceHolderBelowBottom;
  
          if(g_canScrollHorizontally){
              containerInViewPort.querySelector('.main').scrollLeft += evt.deltaY;
          }
      }
  })();
  
  const bulletSlide = document.querySelectorAll('.bullet-slide');
  const bulletGreen = document.querySelectorAll('.bullet-grn');
  const bulletPink = document.querySelectorAll('.bullet-pink');
  
  /*bullet highlight */ 
  window.addEventListener('scroll', ()=&gt; {
    let current = ''; 
    bulletSlide.forEach(bulletSlide =&gt; {
      const sectionTop = bulletSlide.offsetTop;
      const sectionHeight = bulletSlide.clientHeight;
      if (scrollY &gt;= (sectionTop - sectionHeight / 3)) {
        current = bulletSlide.getAttribute('id');
      }
    ;
    })
    bulletGreen.forEach( span =&gt; {
      span.classList.remove('.bullet-grn-active');
      if(span.classList.contains(current)){
        span.classList.add('.bullet-grn-active');
      }
    })
    bulletPink.forEach( span =&gt; {
      span.classList.remove('.bullet-pink-active');
      if(span.classList.contains(current)){
        span.classList.add('.bullet-pink-active');
      }
    })
  })</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>html {
      scroll-behavior: smooth;
  } 
  body {
      min-height: 100%;
      overflow-x: hidden;
  }
  .frame {
      min-height: 100vh;
      min-width: 100vw;
      padding-top: 20px;
  }
  .bg-grn {
      background: #CCD5AE;
  }
  .bg-lp {
      background: #E6C2CB;
  }
  .main {
      overflow-x: hidden;
      display: flex;
      position: sticky;
      top:0;
  }
  .main-slide {  
      min-width: 100vw;
      min-height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
  }
  .wrapper {
      max-width: 600px;
      margin: 0 auto;
  }
  .col-2 { 
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      justify-content: space-between;
      align-items: center;
      gap: 80px;
  }
  .left-div {
      display: flex;
      flex-direction: column;
      gap: 20px;
      justify-content: center;
  }
  .col-1-3 {
      display: grid;
      grid-template-columns: 1fr 2fr;
      align-items: center;
      height: 100%;
      gap: 60px;
  }
  .bullets {
      display: inline-block;
      width: 12px;
      height: 12px;
      border-radius: 50%;
      background-color: white;
      margin-right: 10px;
      cursor: pointer;
  }
  /* divider */
  .divider {
      height: 40vh;
      width: 100vw;
      background-color: #FFD6C2;
      display: flex;
      align-items: center;
  }
  .div-wrapper {
      max-width: 600px;
      margin: 0 auto;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      text-align: center;
      gap: 20px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;section&gt;
      &lt;div class="sticky-container frame bg-grn"&gt;
          &lt;div class="main"&gt;
              &lt;div id="etudes" class="main-slide bullet-slide"&gt; 
                  &lt;div class="col-2 wrapper"&gt;
                      &lt;div class="left-div"&gt;
                          &lt;div class="slide-bullets"&gt;
                              &lt;span class="etudes bullets bullet-grn"&gt;&lt;/span&gt;
                              &lt;span class="micro bullets bullet-grn"&gt;&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;h2 class="title bk"&gt;Les études&lt;/h2&gt;
                          &lt;p&gt;Lorem, ipsum dolor sit amet consectetur adipisicing elit. At hic esse iusto excepturi. Rerum assumenda obcaecati voluptas velit, hic autem!&lt;/p&gt;
                      &lt;/div&gt;
                      &lt;div&gt;
                          &lt;div id="dates-etudes"&gt;
                                  &lt;p&gt;&lt;span class="date"&gt;2017&lt;/span&gt;Master en Marketing et Vente&lt;/p&gt; 
                                  &lt;p&gt;&lt;span class="date"&gt;2015&lt;/span&gt;Licence Langues Étrangères Appliquées&lt;/p&gt;
                                  &lt;p&gt;&lt;span class="date"&gt;2014&lt;/span&gt;DUETI Business and Administration&lt;/p&gt;
                                  &lt;p&gt;&lt;span class="date"&gt;2013&lt;/span&gt;DUT Techniques de Commercialisation&lt;/p&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div id="micro" class="main-slide bullet-slide"&gt;
                  &lt;div class="col-2 wrapper"&gt;
                      &lt;div class="left-div"&gt;
                          &lt;div class="slide-bullets"&gt;
                              &lt;span class="etudes bullets bullet-grn"&gt;&lt;/span&gt;
                              &lt;span class="micro bullets bullet-grn"&gt;&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;h2 class="title bk"&gt;La micro-entreprise&lt;/h2&gt;
                          &lt;p&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Quos error hic totam provident delectus! Aliquid error debitis amet quos eaque?&lt;/p&gt;
                      &lt;/div&gt;
                      &lt;div id="paper-container"&gt;
                          &lt;p class="div-title"&gt;Lorem ipsum, dolor sit amet consectetur adipisicing elit. Itaque, corporis! !&lt;/p&gt;
                          
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/section&gt;
  &lt;div class="divider"&gt;
      &lt;div class="div-wrapper"&gt;
          &lt;h3 class="div-title"&gt;Depuis janvier 2022&lt;/h3&gt;
          &lt;p class="div-p"&gt;Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nisi ipsum eveniet voluptatem quis est nulla soluta ipsam voluptas fugiat odit..&lt;/p&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;section&gt;
      &lt;div class="sticky-container frame bg-lp"&gt;
          &lt;div class="main"&gt;
              &lt;div id="pas1" class="main-slide bullet-slide"&gt;
                  &lt;div class="col-1-3 wrapper"&gt;
                      &lt;div class="left-div"&gt;
                          &lt;div class="slide-bullets"&gt;
                              &lt;span class="pas1 bullets bullet-pink"&gt;&lt;/span&gt;
                              &lt;span class="pas2 bullets bullet-pink"&gt;&lt;/span&gt;
                              &lt;span class="pas3 bullets bullet-pink"&gt;&lt;/span&gt;
                              &lt;span class="pas4 bullets bullet-pink"&gt;&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;h2 class="title"&gt;Mes premiers pas dans le web design&lt;/h2&gt;
                      &lt;/div&gt;
                      &lt;div&gt;
                          &lt;div class="title-para"&gt;
                              &lt;p&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Enim numquam vero recusandae provident, sapiente tempora sequi modi laboriosam velit dolorum!&lt;/p&gt;
                              &lt;div class="bg-pink-ptn"&gt;
                                  &lt;h4&gt;Les axes de réflexion&lt;/h4&gt;
                                  &lt;div class="list col-2"&gt;
                                      &lt;ul&gt;
                                          &lt;li&gt;l'aspect graphique et esthétique&lt;/li&gt;
                                          &lt;li&gt;l'objectif du site&lt;/li&gt;
                                          &lt;li&gt;le contenu &amp; son organisation&lt;/li&gt;
                                      &lt;/ul&gt;
                                      &lt;ul&gt;
                                          &lt;li&gt;les fonctionnalités&lt;/li&gt;
                                          &lt;li&gt;l'évolution prévue&lt;/li&gt;
                                          &lt;li&gt;la gestion du site&lt;/li&gt;
                                      &lt;/ul&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div id="pas2" class="main-slide bullet-slide"&gt;
                  &lt;div class="col-1-3 wrapper"&gt;
                      &lt;div class="left-div"&gt;
                          &lt;div class="slide-bullets"&gt;
                              &lt;span class="pas1 bullets bullet-pink"&gt;&lt;/span&gt;
                              &lt;span class="pas2 bullets bullet-pink"&gt;&lt;/span&gt;
                              &lt;span class="pas3 bullets bullet-pink"&gt;&lt;/span&gt;
                              &lt;span class="pas4 bullets bullet-pink"&gt;&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;h2 class="title"&gt;Mes premiers pas dans le web design&lt;/h2&gt;
                      &lt;/div&gt;
                      &lt;div  class="title-para"&gt;
                          &lt;p&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati consequatur molestias ab maxime, dolor dolorum esse incidunt dolores nulla alias?&lt;/p&gt;
                          &lt;p&gt;Lorem, ipsum dolor sit amet consectetur adipisicing elit. Atque ex perferendis molestias sequi et aliquid nam corrupti quis voluptatum enim.&lt;/span&gt;&lt;/p&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div id="pas3" class="main-slide bullet-slide"&gt;
                  &lt;div class="col-1-3 wrapper"&gt;
                      &lt;div class="left-div"&gt;
                          &lt;div class="slide-bullets"&gt;
                              &lt;span class="pas1 bullets bullet-pink"&gt;&lt;/span&gt;
                              &lt;span class="pas2 bullets bullet-pink"&gt;&lt;/span&gt;
                              &lt;span class="pas3 bullets bullet-pink"&gt;&lt;/span&gt;
                              &lt;span class="pas4 bullets bullet-pink"&gt;&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;h2 class="title"&gt;Mes premiers pas dans le web design&lt;/h2&gt;
                      &lt;/div&gt;
                      &lt;div class="title-para"&gt;
                          &lt;p&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Eos, ratione? Iure deleniti mollitia ea provident labore alias quo minus placeat? Voluptates veritatis praesentium debitis officiis ducimus quae consectetur, qui laudantium modi eveniet aspernatur fugit, veniam rerum dolorum id temporibus provident!&lt;/p&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div id="pas4" class="main-slide bullet-slide"&gt;
                  &lt;div class="col-1-3 wrapper"&gt;
                      &lt;div class="left-div"&gt;
                          &lt;div class="slide-bullets"&gt;
                              &lt;span class="pas1 bullets bullet-pink"&gt;&lt;/span&gt;
                              &lt;span class="pas2 bullets bullet-pink"&gt;&lt;/span&gt;
                              &lt;span class="pas3 bullets bullet-pink"&gt;&lt;/span&gt;
                              &lt;span class="pas4 bullets bullet-pink"&gt;&lt;/span&gt;
                          &lt;/div&gt;
                          &lt;h2 class="title"&gt;Mes premiers pas dans le web design&lt;/h2&gt;
                      &lt;/div&gt;
                      &lt;div class="title-para"&gt;
                          &lt;p&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Numquam molestiae, minima quo voluptas deleniti aliquid blanditiis beatae! Officiis, eos commodi!&lt;/p&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt; </code></pre>
  </div>
  </div>
  </p>