欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 焦点 > pdf.js移动端预览PDF文件时,支持双指缩放

pdf.js移动端预览PDF文件时,支持双指缩放

2025/4/29 18:18:06 来源:https://blog.csdn.net/weixin_45264424/article/details/147588991  浏览:    关键词:pdf.js移动端预览PDF文件时,支持双指缩放

在viewer.html中添加手势缩放代码

<script>// alert("Hello World");let agent = navigator.userAgent.toLowerCase();// if (!agent.includes("iphone")) {let pinchZoomEnabled = false;function enablePinchZoom(pdfViewer) {let startX = 0, startY = 0;let initialPinchDistance = 0;let pinchScale = 1;const viewer = document.getElementById("viewer");const container = document.getElementById("viewerContainer");const reset = () => {startX = startY = initialPinchDistance = 0;pinchScale = 1;};// Prevent native iOS page zoom// document.addEventListener("touchmove", (e) => { if (e.scale !== 1) { e.preventDefault(); } }, { passive: false });document.addEventListener("touchstart", (e) => {if (e.touches.length > 1) {startX = (e.touches[0].pageX + e.touches[1].pageX) / 2;startY = (e.touches[0].pageY + e.touches[1].pageY) / 2;initialPinchDistance = Math.hypot(e.touches[1].pageX - e.touches[0].pageX,e.touches[1].pageY - e.touches[0].pageY);} else {initialPinchDistance = 0;}});document.addEventListener("touchmove",(e) => {if (initialPinchDistance <= 0 || e.touches.length < 2) {return;}if (e.scale !== 1) {e.preventDefault();}const pinchDistance = Math.hypot(e.touches[1].pageX - e.touches[0].pageX,e.touches[1].pageY - e.touches[0].pageY);const originX = startX + container.scrollLeft;const originY = startY + container.scrollTop;pinchScale = pinchDistance / initialPinchDistance;viewer.style.transform = `scale(${pinchScale})`;viewer.style.transformOrigin = `${originX}px ${originY}px`;},{ passive: false });document.addEventListener("touchend", (e) => {if (initialPinchDistance <= 0) {return;}viewer.style.transform = `none`;viewer.style.transformOrigin = `unset`;PDFViewerApplication.pdfViewer.currentScale *= pinchScale;const rect = container.getBoundingClientRect();const dx = startX - rect.left;const dy = startY - rect.top;container.scrollLeft += dx * (pinchScale - 1);container.scrollTop += dy * (pinchScale - 1);reset();});}document.addEventListener("DOMContentLoaded", () => {if (!pinchZoomEnabled) {pinchZoomEnabled = true;enablePinchZoom();}});</script>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词