JS: Touch equivalent for mouseenter

2019-04-05 04:03发布

is there a touch equivalent of the mouseenter.

I would like to detect if user slide on my DIV.

I prefer a solution depending directly on the target element not on a parent element with recounting positions etc.

The site: http://dizzyn.github.io/piano-game/ - works fine with mouse (mouse down and slide; not working with touch slide)

Thank you

2条回答
贪生不怕死
2楼-- · 2019-04-05 04:36

For anyone who is trying to handle touch events in a web app here is helpful documentation W3C - Touch Events which explains the events in detail and how they are handled.

WC3 states:

If a Web application can process touch events, it can intercept them, and no corresponding mouse events would need to be dispatched by the user agent. If the Web application is not specifically written for touch input devices, it can react to the subsequent mouse events instead.

查看更多
放我归山
3楼-- · 2019-04-05 04:38

Look into these events:

touchstart Triggers when the user makes contact with the touch surface and creates a touch point inside the element the event is bound to.

touchmove Triggers when the user moves the touch point across the touch surface.

touchend Triggers when the user removes a touch point from the surface. It fires regardless of whether the touch point is removed while inside the bound-to element, or outside, such as if the user's finger slides out of the element first or even off the edge of the screen.

touchenter Triggers when the touch point enters the bound-to element. This event does not bubble.

touchleave Triggers when the touch point leaves the bound-to element. This event does not bubble.

touchcancel Triggers when the touch point no longer registers on the touch surface. This can occur if the user has moved the touch point outside the browser UI or into a plugin, for example, or if an alert modal pops up.

Specifically touchenter and touchleave.

http://www.javascriptkit.com/javatutors/touchevents.shtml

查看更多
登录 后发表回答