6_移动端事件.html 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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: 100px;
  11. background-color: red;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="box"></div>
  17. <script>
  18. // 获取盒子
  19. var box = document.getElementById("box");
  20. // 鼠标点击事件可以在移动端使用 但是会有延迟时间 300ms 不建议在移动端使用
  21. box.onclick = function(){
  22. console.log("点击了盒子");
  23. }
  24. // 触摸开始事件 当手指触摸到盒子时 触发 没有延迟时间 建议在移动端使用移动端专属事件(只能应用于移动端)
  25. box.ontouchstart = function(){
  26. console.log("触摸开始");
  27. }
  28. // 触摸移动事件 当手指在盒子上移动时 触发 没有延迟时间 建议在移动端使用移动端专属事件(只能应用于移动端)
  29. box.ontouchmove = function(){
  30. console.log("触摸移动");
  31. }
  32. // 触摸结束事件 当手指从盒子上移开时 触发 没有延迟时间 建议在移动端使用移动端专属事件(只能应用于移动端)
  33. box.ontouchend = function(){
  34. console.log("触摸结束");
  35. }
  36. </script>
  37. </body>
  38. </html>