简体   繁体   English

你能阻止 3D 触摸吗?<img> 但不能点击并按住保存?

[英]Can you prevent 3D touch on an <img> but not tap and hold to save?

On <img> tags on iOS mobile Safari, you can tap and hold on an image to bring up a Save Sheet.在 iOS 移动版 Safari 上的<img>标签上,您可以点击并按住图像以调出保存表。 You can also 3D touch to pop/peek it.您还可以通过 3D 触摸来弹出/偷看它。

Can you prevent 3D touch while maintaining the tap and hold functionality?您能否在保持点击和按住功能的同时防止 3D 触摸?

Adding a draggable attribute doesn't work to disable the 3D/force touch.添加可拖动属性不能禁用 3D/强制触摸。 But disabling pointer-events on the works (on iOS 12.2 at least):但是在作品上禁用指针事件(至少在 iOS 12.2 上):

img {
  pointer-events: none;
}

Else you have some documentation about force touch in webkit here: Link to Apple developer site否则你有一些关于 webkit 中强制触摸的文档: 链接到 Apple 开发者网站

Using this CSS stops the prompt from firing on any image and won't disable touch events from buttons or markers with images so they still function as normal.使用此 CSS 会阻止提示在任何图像上触发,并且不会禁用来自按钮或带有图像的标记的触摸事件,因此它们仍能正常运行。

You can still feel the haptic feedback from the touch event when you long press also, you just won't get the prompt/preview.当您长按时,您仍然可以感受到来自触摸事件的触觉反馈,您只是不会得到提示/预览。

#root {
  -webkit-touch-callout: none;
}

You can disable or enable the touch-callout as you wish in your css您可以根据需要在 css 中禁用或启用触摸标注

From HTML with draggable attribute来自具有可拖动属性的 HTML

<img src='yourimage.png' draggable='false'>

Or by preventing default action by browser for dragstart event.或者通过阻止浏览器对dragstart事件的默认操作。 Also by checking touch force.还通过检查触摸力。

document.querySelectorAll('img').forEach(el=> {

    el.addEventListener('dragstart', e => {
        e.preventDefault(); 
        return false; 
    });

    // customize your pressure force
    const force = 0.2; 

    // iOS
    el.addEventListener('touchforcechange', e => {
        if (e.changedTouches[0].force < force) {
            e.preventDefault();
            return false;
        }
    });

    // Standard, Firefox
    el.addEventListener('touchstart', e => {
        if (e.targetTouches[0].force < force) {
            e.preventDefault();
            return false;
        }
    });

});

JSFiddle example JSFiddle示例

If you convert your image to a data-url SVG and embed it through the svg tag then you cannot 3D touch it.如果您将图像转换为 data-url SVG 并通过 svg 标签将其嵌入,那么您将无法 3D 触摸它。 You can also link to other SVG images through the SVG tag.您还可以通过 SVG 标签链接到其他 SVG 图像。 Here is a w3 schools link on how to use the tag: https://www.w3schools.com/html/html5_svg.asp这是一个关于如何使用标签的 w3 学校链接: https : //www.w3schools.com/html/html5_svg.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM