Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to listen to scroll events #249

Open
dingshaohua-com opened this issue Aug 29, 2023 · 4 comments
Open

How to listen to scroll events #249

dingshaohua-com opened this issue Aug 29, 2023 · 4 comments

Comments

@dingshaohua-com
Copy link

How to listen to scroll events

@codiejay
Copy link

Any solution yet?

1 similar comment
@Areeb-dev
Copy link

Any solution yet?

@codiejay
Copy link

codiejay commented Jun 22, 2024

@Areeb-dev I fixed this but forgot how i did it so I just gave my code to GPT to explain as i dont have the time to fully recollect my though process months back. This is what GPT had to say:

The onScrollChange prop of the PdfHighlighter component is responsible for clearing the URL hash whenever the user scrolls. This is done to ensure that any existing hash (used for scrolling to a specific highlight) is removed when the document is scrolled.

<PdfHighlighter
   ...
   onScrollChange={() => {
      // clear URL hash
      if (window.location.hash) {
         window.location.hash = "";
      }
   }}
   ...
/>

PdfHighlighter scrollRef Prop:

The scrollRef prop of the PdfHighlighter component is used to get a reference to the scroll function, which is stored in viewerScrollTo.current. This allows programmatic scrolling to specific highlights when required.

<PdfHighlighter
   ...
   scrollRef={(scrollTo) => {
      viewerScrollTo.current = scrollTo;
   }}
   ...
/>

UseEffect Hook with pdfElem:

This useEffect hook attaches an event listener to the PDF element (pdfElem[0]). This event listener updates the current page number whenever the user scrolls within the PDF. The getPdfInViewPageNumber function is used to determine which page is currently in view.

useEffect(() => {
   if (pdfElem[0]) {
      pdfElem[0].addEventListener("scroll", () => {
         const pdfInViewNum = getPdfInViewPageNumber();
         setCurrentPdfPage(pdfInViewNum);
      });
   }
}, [totalPage, scale]);

In summary, the handling of scroll events on the PDF is managed through the onScrollChange and scrollRef props of the PdfHighlighter component, as well as the useEffect hook that attaches a scroll event listener to the PDF element.

@Areeb-dev
Copy link

Hey @codiejay ,
Basically I want to get page Number when user scroll the pdf so is it possible by using this library or is there any other approach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants