# Quelle est la meilleure façon de se souvenir de la position de défilement sur la page principale (catégories) ?

**URL:** <https://meta.discourse.org/t/what-is-the-best-way-to-remember-the-scroll-position-on-main-categories-page/204561>\
**Category:** Development\
**Created:** [Septembre 27, 2021, 2:47 UTC](https://meta.discourse.org/t/what-is-the-best-way-to-remember-the-scroll-position-on-main-categories-page/204561 "2021-09-27T14:47:37Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![4ong](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/4ong/32/186618_2.png) [@4ong](https://meta.discourse.org/u/4ong)\
**Post date:** [Octobre 4, 2021, 8:45 UTC](https://meta.discourse.org/t/what-is-the-best-way-to-remember-the-scroll-position-on-main-categories-page/204561/4 "2021-10-04T20:45:59Z")

</div>

Une solution rapide et sale pour l’instant – peut-être que cela aidera quelqu’un plus tard.

```plaintext
<script type="text/discourse-plugin" version="0.8">

api.onPageChange((url) => {
    window.removeEventListener("scroll", saveScroll, {passive: true,});
    
    if (url === "/" || url === "/categories"){
        restoreScroll();
        window.addEventListener("scroll", saveScroll, {passive: true,});
    }
});

function restoreScroll() {
    var pos = sessionStorage.getItem("categoriesScrollPosition");
    if (pos != 0) {
        window.scrollTo(0, pos);
    } 
}

function saveScroll() {
    if (window.scrollY != 0) {
        sessionStorage.setItem("categoriesScrollPosition", window.scrollY + 1);
    }
}
</script>

```

---

_[View the full topic](https://meta.discourse.org/t/what-is-the-best-way-to-remember-the-scroll-position-on-main-categories-page/204561)._
