Disable the text-highlighting magnifier on touch-hold on Mobile Safari / Webkit

后端 未结 6 2039
无人共我
无人共我 2020-12-08 09:07

I have some elements in my iPhone website that don\'t have any text in them but require the user to click and hold on them (DIVs). This causes the text-highlighting/editing

相关标签:
6条回答
  • 2020-12-08 09:44

    Add this to the CSS

    body {
    -webkit-touch-callout: none;                /* prevent callout to copy image, etc when tap to hold */
    -webkit-text-size-adjust: none;             /* prevent webkit from resizing text to fit */
    -webkit-user-select: none;                  /* prevent copy paste, to allow, change 'none' to 'text' */}
    
    0 讨论(0)
  • 2020-12-08 09:47

    I found this out while trying it out myself. First of all you have to add this rule to the enclosing element:

    -webkit-user-select: none;
    

    But that, by itself, is not enough on the iPhone. It turns out that the magnifying glass can still appear because, for example, a parent element would accept selection, or just because it feels like it.

    However, I then discovered something cool - if your element adds a touchend and click handler to an element, then Apple's Safari finally avoids the annoying code path that causes the magnifying glass to appear, probably realizing that this element is meant for some UI interaction, and not selecting text. On an equally awesome note, if you do this on elements near the top of the screen, it will also cancel the appearance of the navigation in landscape mode! Not sure however how to cancel the appearance of navigation when clicking on elements on the bottom, does anyone have a solution for that one?

    0 讨论(0)
  • 2020-12-08 09:53

    This is also useful in protecting content that you don't want copied or saved, such as an image:

    #yourdiv img {-webkit-touch-callout: none; }
    
    0 讨论(0)
  • 2020-12-08 09:54

    Just got a response from the Developer Center help desk. I needed to add this CSS rule:

    -webkit-user-select: none;
    
    0 讨论(0)
  • 2020-12-08 10:03

    Use this CSS codes

    -webkit-touch-callout: none;
    -webkit-user-select: none; /* Disable selection/copy in UIWebView */
    
    0 讨论(0)
  • 2020-12-08 10:07

    This solved it for me, in JS:

    document.getElementsByTagName("body")[0].addEventListener("touchstart",
     function(e) { e.returnValue = false });
    

    Seems to bypass whatever the OS has in there to catch the touch.

    0 讨论(0)
提交回复
热议问题