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

Calculate scrollTo inner content #61

Open
alquist42 opened this issue Aug 21, 2023 · 3 comments
Open

Calculate scrollTo inner content #61

alquist42 opened this issue Aug 21, 2023 · 3 comments
Labels
question Further information is requested

Comments

@alquist42
Copy link

Hey, such a great work!!

Can you please advice how would be possible to scroll to the elements inside viewer?

For example we have some divs inside:
Screen Shot 2023-08-21 at 15 49 10

and when clicking on number of the div - it brings into viewport regardless of scroll position, zoom or div position
Screen Shot 2023-08-21 at 15 49 48

Demo: https://69252v.csb.app/

Thanks in advance!

@daybrush
Copy link
Owner

@alquist42

use .setTo method with offset

function navigateToPage(pageId) {
  alert(pageId);
  const viewportElement = infiniteViewer.getViewport();
  const pageElement = document.querySelector(`[id="${pageId}"]`);
  const viewportRect = viewportElement.getBoundingClientRect();
  const pageRect = pageElement.getBoundingClientRect();
  const zoom = infiniteViewer.getZoom();
  const offsetLeft = (pageRect.left - viewportRect.left) / zoom;
  const offsetTop = (pageRect.top - viewportRect.top) / zoom;
  const pageWidth = pageRect.width / zoom;
  const pageHeight = pageRect.height / zoom;
  const nextZoom = 2;

  infiniteViewer.setTo({
    x: - infiniteViewer.getContainerWidth() / 2 / nextZoom + offsetLeft + pageWidth / 2,
    y: -infiniteViewer.getContainerHeight() / 2 / nextZoom + offsetTop + pageHeight / 2,
    zoom: nextZoom,
    duration: 300,
  });
}

@daybrush daybrush added the question Further information is requested label Aug 26, 2023
@alquist42
Copy link
Author

Just big thanks! :)

@dubem-design
Copy link

Any way to implement zoomToFit or ZoomToFill?
@daybrush

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

No branches or pull requests

3 participants