title: Getting back to a CSS position sticky element with JavaScript?
body: <p>I have a page where all sections fill the entire screen and are positioned with CSS <code>position: sticky;</code> in order to achieve a <em>layered</em> effect. See here:</p>
  
  <p><a href="https://codesandbox.io/s/ecstatic-khayyam-cgql1?fontsize=14&amp;hidenavigation=1&amp;theme=dark" rel="nofollow noreferrer">https://codesandbox.io/s/ecstatic-khayyam-cgql1?fontsize=14&amp;hidenavigation=1&amp;theme=dark</a></p>
  
  <p>This all works great and as expected.</p>
  
  <p>The problem comes however from navigating between these areas with JavaScript. If you try using the menu, you can see that the links will work on sections that we haven't fully gotten to yet (and therefore not "sticky") but does not allow you to "go back up" the page.</p>
  
  <p>I believe this is an issue with <code>el.getBoundingClientRect()</code> once an element has become "sticky", its top value becomes essentially always zero.</p>
  
  <p>Here I am using a small library called <code>Jump.js</code> to jump around but even in vanilla JS this issue would still be the same, as the problem is a result of the calculation from when the element becomes sticky. </p>
  
  <p>Is there any way to find the original position for each section before it was sticky? At least that way I could navigate the user by setting the scroll position manually.</p>
  
  <p>I am using Vue.js but that does not affect the issue at hand which is CSS and JS related.</p>
  
  <p><strong>App.vue</strong></p>
  
  <pre><code>&lt;template&gt;
    &lt;main id="app"&gt;
      &lt;ul class="menu"&gt;
        &lt;li @click="goTo('A')"&gt;Section A&lt;/li&gt;
        &lt;li @click="goTo('B')"&gt;Section B&lt;/li&gt;
        &lt;li @click="goTo('C')"&gt;Section C&lt;/li&gt;
        &lt;li @click="goTo('D')"&gt;Section D&lt;/li&gt;
        &lt;li @click="goTo('E')"&gt;Section E&lt;/li&gt;
      &lt;/ul&gt;
      &lt;SectionItem id="A" color="red"/&gt;
      &lt;SectionItem id="B" color="green"/&gt;
      &lt;SectionItem id="C" color="blue"/&gt;
      &lt;SectionItem id="D" color="orange"/&gt;
      &lt;SectionItem id="E" color="purple"/&gt;
    &lt;/main&gt;
  &lt;/template&gt;
  
  &lt;script&gt;
  import jump from "jump.js";
  import SectionItem from "./components/SectionItem.vue";
  
  export default {
    name: "App",
    components: {
      SectionItem
    },
    methods: {
      goTo(id) {
        jump(`#${id}`, {
          duration: 300
        });
      }
    }
  };
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>SectionItem.vue</strong></p>
  
  <pre><code>&lt;template&gt;
    &lt;div :id="id" class="SectionItem" :style="styles"&gt;
      &lt;p&gt;I am section item: {{ id }}&lt;/p&gt;
    &lt;/div&gt;
  &lt;/template&gt;
  
  &lt;script&gt;
  export default {
    name: "SectionItem",
    props: {
      id: {
        type: String,
        required: true
      },
      color: {
        type: String,
        required: true
      }
    },
    computed: {
      styles() {
        return {
          backgroundColor: this.color
        };
      }
    }
  };
  &lt;/script&gt;
  
  &lt;style&gt;
  .SectionItem {
    position: sticky;
    top: 0;
    width: 100%;
    min-height: 100vh;
    padding: 20px;
    color: white;
    border: 10px solid white;
  }
  &lt;/style&gt;
  </code></pre>
  
  <p>I'm looking for any reasonable solutions that get get the auto-scrolling to work in both directions. Many thanks for your time.</p>