question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Window scrolls to top onClick

See original GitHub issue

I am using links for Previous/Next of PDF pages.

The functionality works great (pages go previous/next as expected). However, my viewport/window scrolls to the top anytime the onClick link is clicked. Would appreciate any advice on how to prevent the page window from scrolling. Thanks!

<a onClick={pageNumber <= 1 ? "event.stopPropagation()" : previousPage}>&larr; Previous</a>

references:


    function previousPage() {
        changePage(-1);
    }

    function nextPage() {
        changePage(1);
    }

Thanks in advance. One of my absolute favorite React projects!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
kubamicommented, Nov 25, 2020

The same thing happened to me. But it only happened when I navigated to a page that hasn’t been displayed before in this session. When changing to a page that was already displayed it did not scroll to the top.

React will scroll viewport to the top when components change dimensions on the page. The solution was to set width or height on the CSS class of the container Document. (not to be confused with the width or height of the Page)

0reactions
hdehalcommented, Aug 28, 2021

As a heads-up, a combination of CSS and the methods in these threads worked for me: https://github.com/wojtekmaj/react-pdf/issues/418 https://github.com/wojtekmaj/react-pdf/issues/689

.react-pdf__Page.prevPage {
  position: absolute !important;
  z-index: 1;
}

.react-pdf__Document{
  height: 80px;
}

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scroll To Top When DIV Clicked? [closed] - Stack Overflow
We all know that scroll to top with named anchors via HTML is possible but is it possible to scroll to the top...
Read more >
How To Create a Scroll Back To Top Button - W3Schools
This example demonstrates how to create a "scroll to top" button that becomes visible when the user starts to scroll the page.
Read more >
Scroll to the top of the page using JavaScript/jQuery
A scrollable page can be scrolled to the top using 2 approaches: Method 1: Using window.scrollTo(): The scrollTo() method of the window ......
Read more >
Window.scroll() - Web APIs - MDN Web Docs
The Window.scroll() method scrolls the window to a particular place in the document.
Read more >
Scroll to the top of the Page in React.js | bobbyhadz
The `scrollTo` method on the `window` object scrolls to a particular set of ... scroll to top on button click */} <button onClick={()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found