touchHandle.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. export default {
  2. data(){
  3. return {
  4. startX:0,
  5. startY:0,
  6. clientX:0,
  7. clientY:0,
  8. }
  9. },
  10. methods:{
  11. mytouchstart(e) {
  12. this.clientX = 0;
  13. this.clientY = 0;
  14. this.startX = e.changedTouches[0].clientX;
  15. this.startY = e.changedTouches[0].clientY;
  16. },
  17. mytouchmove(e) {
  18. let startX = this.startX // 开始x坐标
  19. let startY = this.startY //开始y坐标
  20. let touchMoveX = e.changedTouches[0].clientx //滑动变化坐标
  21. let touchMoveY = e.changedTouches[0].clientY //滑动变化坐标
  22. this.clientX = touchMoveX - startX;
  23. this.clientY = touchMoveY - startY;
  24. },
  25. mytouchend(e) {
  26. let startX = this.startX; // 开始x坐标
  27. let startY = this.startY; //开始y坐标
  28. let touchMoveX = e.changedTouches[0].clientX; //滑动变化坐标
  29. let touchMoveY = e.changedTouches[0].clientY; //滑动变化坐标
  30. let angle = this.angle(
  31. {
  32. X: startX,
  33. Y: startY
  34. }, {
  35. X: touchMoveX,
  36. Y: touchMoveY
  37. })
  38. //滑动角度超过45retrun
  39. // console.log(Math.abs(angle), "Math.abs(angle)")
  40. if (Math.abs(angle) > 45){//上下滑动
  41. if (touchMoveY > startY && (touchMoveY - startY)>20) { //下滑
  42. console.warn("***touchend-下滑***",touchMoveY - startY);
  43. this.downScroll && this.downScroll()
  44. }
  45. if (startY > touchMoveY && (startY - touchMoveY)>20) { //上滑
  46. console.warn("***touchend-上滑***",startY - touchMoveY);
  47. this.upScroll && this.upScroll()
  48. }
  49. }else{//左右滑动
  50. if (startX > touchMoveX && (startX - touchMoveX)>50) { //左滑
  51. console.warn("***touchend-左滑***",startX - touchMoveX);
  52. this.leftScroll && this.leftScroll()
  53. }
  54. if(touchMoveX > startX && (touchMoveX - startX)>50){//右滑
  55. console.warn("***touchend-右滑***",touchMoveX - startX);
  56. this.rightScroll && this.rightScroll()
  57. }
  58. }
  59. },
  60. angle(start,end){
  61. let _X = end.X - start.X;
  62. let _Y = end.Y - start.Y;
  63. if(_X == 0) return 90;
  64. return 360 * Math.atan((_Y / _X) / (2*Math.PI))
  65. },
  66. }
  67. }