Skip to content
Advertisement

How to make a draggable for touch?

I want the orange rectangle to be draggable using mouse or touch. The function for the mouse is working for me, so I tried copying it and replacing mousedown with ontouchstart, mousemove with ontouchmove and mouseup with ontouchend but it doesn’t seem to move. Any suggestions? Thanks!

JavaScript
JavaScript
JavaScript

Advertisement

Answer

For one, the names of your events are incorrect. Omit the on prefix.

Second, touchmove works a little different from mousemove. The event parameter that gets passed to touchmove does not have a clientX or clientY property. Instead it contains a TouchList that needs to be iterated. See below:

JavaScript
JavaScript
JavaScript

For more information see Touch Events and Using Touch Events.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement