Search code examples
javascriptandroidmousemoveonmousemovetouchmove

Continuous ontouchmove?


How do I make this work on my Android mobile continuously, similarly to the way it works on my PC? Currently it doesn't:

<!DOCTYPE html>
<html>
  <head>
    <script>
(function() {
  document.onmousemove = handleMove; // for PC
  document.ontouchmove = handleMove; // for Mobile
  function handleMove(event) {
    document.getElementById('myDiv').innerHTML = event.pageX + "/" + event.pageY;
  }
})();
    </script>
  </head>
  <body>
    <div id="myDiv"></div>
  </body>
</html>

Solution

  • On pc theres MouseEvent and on mobile TouchEvent. Create a separate function to handle mobile events.

    function handleMobile(event) {
     document.getElementById('myDiv').innerHTML = event.touches[0].pageX + "/" + event.touches[0].pageY;
    }