How do I get the scrolling element of current web page?

86 Views Asked by At

If the route changes, I want to find the element with scroll on the page I moved and give it window.scrollTo(0,0), how should I find the element?

current code

if (process.client) {
    router.afterEach((to, from) => {
      document.querySelector('.overflow-scroll').scrollTo(0, 0)
    })
  }

The code work well but, I don't want use document.querySelector. I want to find scrolling element when route!

1

There are 1 best solutions below

0
Bhavesh Sharma On

Make an event listener and a parameter "scroll" in the event listener write predefined functions or keywords just like scrollX or scrollY You can aslo use bottom or top.

scroller.addEventListener("scroll", event => {
  output.textContent = `scrollBy: ${scroller.scrollBy}`;
});

Here is scroll is html class and Id html elements you want to scroll. And output is just show much px you scroll it he is **html class or Id ** you can remove it.

To know or learn in depth.

https://www.w3.org/TR/2016/WD-cssom-view-1-20160317/#dom-element-scrolltop