Disable longPress action on mobile with javascript

核能气质少年 提交于 2021-02-04 16:32:23

问题


in this period,I make some new webApps, but I have a big problem with drag and drop. I write a file manager in javascript, but when on mobile (smartphone, tablet andorid or iOs) I try to work drag and drop, the phone show me the longPress menu (on folder icon for examample) for copy url or image. there is some way in JS to disable longPress on mobile?

load image via css, isn't a valid solutions for me.


回答1:


-webkit-touch-callout: none;                /* prevent callout to copy image, etc when tap to hold */
    -webkit-user-select: none;                  /* prevent copy paste, to allow, change 'none' to 'text' */



回答2:


check the value of navigator.userAgent and compare with android|iphone|ipad etc and in your java script you could do the following

function init() {
  onLongPress(document.getElementById('element'));
}

function onLongPress(node) {
  node.ontouchstart = nullEvent;
  node.ontouchend = nullEvent;
  node.ontouchcancel = nullEvent;
  node.ontouchmove = nullEvent;
}

function nullEvent(event) {
  var e = event || window.event;
  e.preventDefault && e.preventDefault();
  e.stopPropagation && e.stopPropagation();
  e.cancelBubble = true;
  e.returnValue = false;
  return false;
}


来源:https://stackoverflow.com/questions/17297988/disable-longpress-action-on-mobile-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!