[关闭]
@sammffl 2016-07-15T07:39:57.000000Z 字数 2230 阅读 723

tap点击逻辑

零时文件


  1. require.config({
  2. paths: {
  3. zepto: 'zepto.min'
  4. }
  5. });
  6. define(['zepto'], function ($) {
  7. var d = {
  8. SWIPE_DISTANCE: 20, //移动30px之后才认为swipe事件
  9. SWIPE_TIME: 500, //
  10. positions: [],
  11. getId: function (ele) {
  12. return $(ele).attr('id')
  13. },
  14. getTouchPos: function (e) {
  15. var touches = e.touches;
  16. if (touches && touches[0]) {
  17. return {
  18. x: touches[0].clientX,
  19. y: touches[0].clientY
  20. };
  21. }
  22. return {x: e.clientX, y: e.clientY};
  23. },
  24. //计算两点之间距离
  25. getDist: function (p1, p2) {
  26. if (!p1 || !p2) return 0;
  27. return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
  28. },
  29. //计算两点之间所成角度
  30. getAngle: function (p1, p2) {
  31. var r = Math.atan2(p2.y - p1.y, p2.x - p1.x);
  32. var a = r * 180 / Math.PI;
  33. return a;
  34. },
  35. //获取swipe的方向
  36. getSwipeDirection: function (p2, p1) {
  37. var dx = p2.x - p1.x;
  38. var dy = -p2.y + p1.y;
  39. console.log('p2.y', p2.y, 'p1.y', p1.y);
  40. console.log(dx, dy);
  41. var angle = Math.atan2(dy, dx) * 180 / Math.PI;
  42. if (angle < 45 && angle > -45) {
  43. return "right";
  44. }
  45. if (angle >= 45 && angle < 135) {
  46. return "top";
  47. }
  48. if (angle >= 135 || angle < -135) return "left";
  49. if (angle >= -135 && angle <= -45) return "bottom";
  50. },
  51. touchStart: function (e) {
  52. //console.dir(e);
  53. //console.log($(this).parent('li').attr('id'));
  54. //console.log(app.pos)
  55. var id = d.getId($(this));
  56. d.positions[id] = {
  57. point_start: d.getTouchPos(e),
  58. time_start: Date.now()
  59. };
  60. //console.log(d.positions)
  61. //
  62. //console.log(d.positions['1'])
  63. //app.pos.push()
  64. },
  65. touchMove: function (e) {
  66. //console.dir(e)
  67. var id = d.getId($(this));
  68. d.positions[id]["point_end"] = d.getTouchPos(e)
  69. //console.dir(d.positions['1']['point_end'])
  70. //e.preventDefault();
  71. },
  72. touchEnd: function (e) {
  73. var id = d.getId($(this));
  74. //console.dir(e);
  75. d.positions[id]["time_end"] = Date.now();
  76. console.log(d.getDist(d.positions[id]['point_start'], d.positions[id]['point_end']))
  77. if (d.getDist(d.positions[id]['point_start'], d.positions[id]['point_end']) > d.SWIPE_DISTANCE) {
  78. //&& d.positions[id]['time_end'] - d.positions[id]['time_start'] > d.SWIPE_TIME) {
  79. var dir = d.getSwipeDirection(d.positions[id]['point_end'], d.positions[id]['point_start']);
  80. console.log(dir);
  81. } else if (d.positions[id]['time_end'] - d.positions[id]['time_start'] < d.SWIPE_TIME) {
  82. console.log('click')
  83. location.href='https://www.baidu.com';
  84. } else {
  85. console.log('click long time')
  86. }
  87. console.log(d.positions[id]);
  88. },
  89. init: function () {
  90. var self = this;
  91. //var p = self.pos;
  92. $('li').bind('touchstart', self.touchStart);
  93. $('li').bind('touchmove', self.touchMove);
  94. $('li').bind('touchend', self.touchEnd);
  95. }
  96. }
  97. return d;
  98. //console.log($("div"))
  99. });
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注