四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.5 KiB

  1. const threshold = 50
  2. export default {
  3. props: {
  4. //点击事件位置设置
  5. clickOption: {
  6. type: Object,
  7. default () {
  8. return {
  9. width: uni.upx2px(200),
  10. height: uni.upx2px(200),
  11. left: 'auto',
  12. top: 'auto'
  13. }
  14. }
  15. }
  16. },
  17. computed: {
  18. //点击区域左边位置
  19. clickLeft () {
  20. return typeof this.clickOption.left == 'number' ? this.clickOption.left : (this.windowWidth / 2) - (this.clickOption.width / 2)
  21. },
  22. //点击区域右边位置
  23. clickRight () {
  24. return this.clickLeft + this.clickOption.width
  25. },
  26. //点击区域顶部位置
  27. clickTop () {
  28. return typeof this.clickOption.top == 'number' ? this.clickOption.top : (this.windowHeight / 2) - (this.clickOption.height / 2)
  29. },
  30. //点击区域底部位置
  31. clickBottom () {
  32. return this.clickTop + this.clickOption.height
  33. }
  34. },
  35. data () {
  36. return {
  37. autoplaySync: false,
  38. windowHeight: 0,//组件高度
  39. windowWidth: 0,//组件宽度
  40. clickTime: 0,//点击次数
  41. touchstartX: 0,//触摸开始x轴位置
  42. touchstartY: 0,//触摸开始y轴位置
  43. touchmoveX: 0,//触摸滑动x轴距离
  44. touchmoveY: 0,//触摸滑动y轴距离
  45. touchTime: 0//触摸时间
  46. }
  47. },
  48. methods: {
  49. touchstart (e) {
  50. if ( this.pageType != 'scroll' ) this.autoplaySync = false//如果是翻页阅读关闭自动播放
  51. if ( this.touchTimer ) {//清楚定时任务
  52. clearTimeout(this.touchTimer)
  53. this.touchTimer = null
  54. }
  55. if ( this.longTimer ) {//清楚定时任务
  56. clearTimeout(this.longTimer)
  57. this.longTimer = null
  58. }
  59. const touch = e.touches[0]
  60. this.touchstartX = touch.pageX
  61. this.touchstartY = touch.pageY
  62. //点击在规定区域中
  63. if ( this.touchstartX >= this.clickLeft && this.touchstartX <= this.clickRight && this.touchstartY >= this.clickTop && this.touchstartY <= this.clickBottom) {
  64. this.clickTime++
  65. if ( this.clickTime == 1 ) {//第一次点击
  66. this.touchTimer = setTimeout(() => {
  67. this.touchTime = 300
  68. }, 300)
  69. this.longTimer = setTimeout(() => {
  70. this.touchTime = 500
  71. if ( this.touchmoveX <= threshold && this.touchmoveY <= threshold ) this.$emit('long-click')//抛出长按事件
  72. this.resetTouch()
  73. }, 500)
  74. }
  75. } else {//没有点击在规定区域中直接重置触摸事件
  76. this.resetTouch()
  77. }
  78. },
  79. touchmove (e) {
  80. if ( this.clickTime > 0 ) {
  81. const touch = e.touches[0]
  82. this.touchmoveX = Math.abs(touch.pageX - this.touchstartX)
  83. this.touchmoveY = Math.abs(touch.pageY - this.touchstartY)
  84. }
  85. },
  86. touchend (e) {
  87. if ( this.pageType != 'scroll' ) this.autoplaySync = this.autoplay//如果是横向翻页开启自动播放
  88. if ( this.touchTimer ) {//清楚按下定时任务
  89. clearTimeout(this.touchTimer);
  90. this.touchTimer = null
  91. }
  92. if ( this.longTimer ) {//清楚长按定时任务
  93. clearTimeout(this.longTimer)
  94. this.longTimer = null
  95. }
  96. //点击次数为零或者触摸时间超过300,或者滑动距离超过阙值
  97. if ( this.clickTime == 0 || this.touchTime > 300 || this.touchmoveX > threshold || this.touchmoveY > threshold ) {
  98. this.resetTouch()
  99. return
  100. }
  101. if ( this.clickTime == 1 ) {//第一次点击
  102. this.touchTimer = setTimeout(() => {
  103. this.$emit('single-click')//抛出单击事件
  104. this.resetTouch()
  105. }, 300)
  106. }
  107. if ( this.clickTime == 2 ) {//第二次点击
  108. this.$emit('double-click')//抛出双击事件
  109. this.resetTouch()
  110. }
  111. },
  112. resetTouch () {
  113. this.touchstartX = 0
  114. this.touchstartY = 0
  115. this.touchmoveX = 0
  116. this.touchmoveY = 0
  117. this.touchTime = 0
  118. this.clickTime = 0
  119. }
  120. }
  121. }