7.移动端事件.html 1.3 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: #0f0;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="box"></div>
  17. <!--
  18. touchstart:手指触摸到一个 DOM 元素时触发。
  19. touchmove:手指在一个 DOM 元素上滑动时触发。
  20. touchend:手指从一个 DOM 元素上移开时触发。
  21. 每个触摸事件都包括了三个触摸列表,每个列表里包含了对应的一系列触摸点(用来实现多点触控)
  22. touches:当前位于屏幕上的所有手指的列表。
  23. targetTouches:位于当前DOM元素上手指的列表。
  24. changedTouches:涉及当前事件手指的列表
  25. click会有200-300ms延迟
  26. -->
  27. <script>
  28. let box = document.getElementById("box");
  29. box.ontouchstart = function(event) {
  30. console.log('开始',event)
  31. }
  32. box.ontouchmove = function(event) {
  33. console.log('移动',event)
  34. }
  35. box.ontouchend = function(event) {
  36. console.log('离开',event)
  37. }
  38. </script>
  39. </body>
  40. </html>