简体   繁体   English

长按移动浏览器后的鼠标向上事件

[英]mouse up event after long press on mobile browser

I have a button when pressed or on mouse-down event on it sends a command. 我有一个按钮,当按下按钮或鼠标按下事件时会发送命令。 It should also send a command when the button is released(as we don't have any release event, to my knowledge), I am using mouse-up event on the button. 当按钮被释放时,它也应该发送一个命令(据我所知,因为我们没有任何释放事件),所以我在按钮上使用了鼠标向上事件。 When i use the long press on the button from computer browser the mouse-up event works, But when i use mobile browser, if i do a long press on it the mouse up event is not fired, as the mobile browser will have text selection feature on long press. 当我从计算机浏览器中长按按钮时,鼠标向上事件有效;但是当我使用移动浏览器时,如果长按它,则不会触发鼠标向上事件,因为移动浏览器将具有文本选择功能长按功能。 Could some one help me with this. 有人可以帮我这个忙吗?

When the user interacts with your application using a mouse, it will respond via 'click' event, but when the user uses touch enable devices and touches the screen both 'touch' and 'click' event will occur. 当用户使用鼠标与您的应用程序交互时,它将通过“单击”事件做出响应,但是当用户使用触摸启用设备并触摸屏幕时,将同时发生“触摸”和“单击”事件。 for the single touch following events will occur in order : 对于单点触摸,将依次发生以下事件:

  1. touchstart 触摸开始
  2. touchmove 触摸移动
  3. touchend 触摸端
  4. mouseover 鼠标移到
  5. mousemove 鼠标移动
  6. mousedown 按下鼠标
  7. mouseup 鼠标向上
  8. click 点击

one other 'touchcancel' will occur if the touch is interrupted. 如果触摸中断,则会发生另一次“触摸取消”。

When the user touches the screen, mouse events also executes. 当用户触摸屏幕时,鼠标事件也会执行。 To avoid this, stop the default actions of touch events using preventDefault() method of event handler object,(e.preventDefault(); where 'e' is the event handler object). 为避免这种情况,请使用事件处理程序对象的preventDefault()方法(e.preventDefault();其中“ e”是事件处理程序对象)停止触摸事件的默认操作。

Example : 范例:

 let timeIn, timeOut; const touchStart=(e)=>{ e.preventDefault(); console.log('touch start'); timeIn = Date.now(); } const touchMove=(e)=>{ e.preventDefault(); timeOut= Date.now(); console.log('touch move'); } const touchEnd=(e)=>{ e.preventDefault(); timeOut=((Date.now()-timeIn)/1000).toFixed(2); console.log('touch end' , timeOut); } const mouseOver=()=>{ console.log('mouse over'); } const mouseMove=()=>{ console.log('mouse move'); } const mouseUp=()=>{ console.log('mouse up'); } const mouseDown=()=>{ console.log('mouse down'); } const mouseClick=()=>{ console.log('mouse click'); } const touchCancel=(e)=>{ console.log('touch interrupted') } 
 <div ontouchstart="touchStart(event)" ontouchmove="touchMove(event)" ontouchend="touchEnd(event)" onmouseover="mouseOver(event)" onmousemove="mouseMove(event)" onmouseup="mouseUp(event)" onmousedown="mouseDown(event)" onclick="mouseClick(event)" ontouchcancel="touchCancel(event)" > touch me </div> 

To test this code on codepane : https://codepen.io/omiGit/pen/MVapRO 要在代码窗格上测试此代码,请执行以下操作: https ://codepen.io/omiGit/pen/MVapRO

There is a good article on touch and mouse, must read: https://www.html5rocks.com/en/mobile/touchandmouse 关于触摸和鼠标,有一篇很好的文章,必须阅读: https : //www.html5rocks.com/en/mobile/touchandmouse

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

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