如何在含svg-pan-zoom的页面实现双指滑动滚动,同时保留单指SVG平移功能
实现双指滚动页面+单指平移SVG的解决方案
没问题,这个需求完全可以实现!核心思路是根据触摸手指的数量动态切换SVG交互的启用状态:当检测到双指操作时,暂时禁用SVG的pan和pinch功能,让页面接管滚动;单指操作时再恢复SVG的交互权限。
下面是修改后的完整代码,我已经帮你调整了关键逻辑:
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <script src="https://bumbu.me/svg-pan-zoom/dist/svg-pan-zoom.js"></script> <script src="http://bumbu.me/svg-pan-zoom/demo/hammer.js"></script> </head> <!-- Modified from http://bumbu.me/svg-pan-zoom/demo/mobile.html --> <body> <p> Of on affixed civilly moments promise explain fertile in. Assurance advantage belonging happiness departure so of. Now improving and one sincerity intention allowance commanded not. Oh an am frankness be necessary earnestly advantage estimable extensive. Five he wife gone ye. Mrs suffering sportsmen earnestly any. In am do giving to afford parish settle easily garret. </p><p> Up unpacked friendly ecstatic so possible humoured do. Ample end might folly quiet one set spoke her. We no am former valley assure. Four need spot ye said we find mile. Are commanded him convinced dashwoods did estimable forfeited. Shy celebrated met sentiments she reasonably but. Proposal its disposed eat advanced marriage sociable. Drawings led greatest add subjects endeavor gay remember. Principles one yet assistance you met impossible. </p><p> Spoke as as other again ye. Hard on to roof he drew. So sell side ye in mr evil. Longer waited mr of nature seemed. Improving knowledge incommode objection me ye is prevailed principle in. Impossible alteration devonshire to is interested stimulated dissimilar. To matter esteem polite do if. </p><p> It if sometimes furnished unwilling as additions so. Blessing resolved peculiar fat graceful ham. Sussex on at really ladies in as elinor. Sir sex opinions age properly extended. Advice branch vanity or do thirty living. Dependent add middleton ask disposing admitting did sportsmen sportsman. </p> <div id="mobile-div" style="width: 602px; height: 420px; border:1px solid black; "> <svg id="mobile-svg" xmlns="http://www.w3.org/2000/svg" style="display: inline; width: inherit; min-width: inherit; max-width: inherit; height: inherit; min-height: inherit; max-height: inherit;" version="1.1"> <defs> <linearGradient id="linear-gradient" x1="0%" y1="0%" x2="0%" y2="100%"> <stop offset="0%" style="stop-color:rgb(56,121,217);stop-opacity:1" /> <stop offset="100%" style="stop-color:rgb(138,192,7);stop-opacity:1" /> </linearGradient> </defs> <g fill="none"> <g stroke="#000" fill="#FFF"> <rect x="5" y="5" width="240" height="240" fill="url(#linear-gradient)"/> <path d="M 5 5 L 245 245 Z"/> </g> </g> </svg> </div> <p> Of on affixed civilly moments promise explain fertile in. Assurance advantage belonging happiness departure so of. Now improving and one sincerity intention allowance commanded not. Oh an am frankness be necessary earnestly advantage estimable extensive. Five he wife gone ye. Mrs suffering sportsmen earnestly any. In am do giving to afford parish settle easily garret. </p><p> Up unpacked friendly ecstatic so possible humoured do. Ample end might folly quiet one set spoke her. We no am former valley assure. Four need spot ye said we find mile. Are commanded him convinced dashwoods did estimable forfeited. Shy celebrated met sentiments she reasonably but. Proposal its disposed eat advanced marriage sociable. Drawings led greatest add subjects endeavor gay remember. Principles one yet assistance you met impossible. </p><p> Spoke as as other again ye. Hard on to roof he drew. So sell side ye in mr evil. Longer waited mr of nature seemed. Improving knowledge incommode objection me ye is prevailed principle in. Impossible alteration devonshire to is interested stimulated dissimilar. To matter esteem polite do if. </p><p> It if sometimes furnished unwilling as additions so. Blessing resolved peculiar fat graceful ham. Sussex on at really ladies in as elinor. Sir sex opinions age properly extended. Advice branch vanity or do thirty living. Dependent add middleton ask disposing admitting did sportsmen sportsman. </p> <script> window.onload = function() { // 初始化svg-pan-zoom实例 const svgPanZoomInstance = svgPanZoom('#mobile-svg', { zoomEnabled: true, panEnabled: true, controlIconsEnabled: false, fit: false, center: false, // 用hammer作为手势处理器 eventsHandler: { haltEventListeners: ['touchstart', 'touchend', 'touchmove', 'touchleave', 'touchcancel'] } }); // 获取SVG元素的hammer实例 const hammerInstance = svgPanZoomInstance.getHammer(); // 启用pan和pinch手势 hammerInstance.get('pan').set({ enable: true }); hammerInstance.get('pinch').set({ enable: true }); // 监听touchstart事件,根据手指数量切换交互状态 document.addEventListener('touchstart', function(e) { if (e.touches.length >= 2) { // 双指操作:禁用SVG的平移和缩放,让页面接管滚动 svgPanZoomInstance.enablePan(false); svgPanZoomInstance.enableZoom(false); // 阻止SVG的事件冒泡,确保页面能捕获滚动事件 e.stopPropagation(); } else { // 单指操作:恢复SVG的交互权限 svgPanZoomInstance.enablePan(true); svgPanZoomInstance.enableZoom(true); } }, { passive: false }); // 监听touchend事件,确保单指操作时恢复SVG交互 document.addEventListener('touchend', function(e) { if (e.touches.length <= 1) { svgPanZoomInstance.enablePan(true); svgPanZoomInstance.enableZoom(true); } }); } </script> </body> </html>
关键逻辑说明:
- 动态切换SVG交互:通过
enablePan()和enableZoom()方法,根据触摸点数量实时调整SVG的交互状态。双指时禁用SVG操作,让页面滚动;单指时恢复,允许平移SVG。 - 事件冒泡控制:双指触摸时阻止事件向SVG元素冒泡,确保页面能捕获到滚动事件,避免被SVG的pan操作拦截。
- viewport优化:移除了
user-scalable=no,避免限制页面的双指滚动行为(如果需要限制页面缩放,可以重新添加,但不影响双指滚动功能)。
这样就能完美实现:单指触摸SVG时平移内容,双指触摸任意区域(包括SVG)时滚动整个页面。
内容的提问来源于stack exchange,提问作者ACProctor
相关产品推荐
相关产品推荐

