infinite-viewer icon indicating copy to clipboard operation
infinite-viewer copied to clipboard

Calculate `scrollTo` inner content

Open alquist42 opened this issue 2 years ago • 3 comments

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!

alquist42 avatar Aug 21 '23 13:08 alquist42

@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 avatar Aug 26 '23 10:08 daybrush

Just big thanks! :)

alquist42 avatar Aug 29 '23 18:08 alquist42

Any way to implement zoomToFit or ZoomToFill? @daybrush

dubem-design avatar Apr 03 '24 09:04 dubem-design