7.touch.html 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. #box {
  9. width: 200px;
  10. height: 200px;
  11. background-color: #ff0;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <!--
  17. touchstart:手指触摸到一个 DOM 元素时触发。
  18. touchmove:手指在一个 DOM 元素上滑动时触发。
  19. touchend:手指从一个 DOM 元素上移开时触发。
  20. 每个触摸事件都包括了三个触摸列表,每个列表里包含了对应的一系列触摸点(用来实现多点触控)
  21. touches:当前位于屏幕上的所有手指的列表。
  22. targetTouches:位于当前DOM元素上手指的列表。
  23. changedTouches:涉及当前事件手指的列表
  24. Tap:轻敲
  25. click与tap都会出发点击事件,但是在手机web端,click会有200-300ms延迟,所以一般用tap(轻击)代替click作为点击事件。singleTap 和 doubleTap分别代表单击和双击。
  26. -->
  27. <div id="box"></div>
  28. <script>
  29. var box = document.querySelector('#box');
  30. box.ontouchstart = function(event) {
  31. console.log(event,'touchstart');
  32. }
  33. box.ontouchmove = function(event) {
  34. console.log('touchmove',event);
  35. }
  36. box.ontouchend = function(event) {
  37. console.log('touchend',event);
  38. }
  39. </script>
  40. </body>
  41. </html>