swipedelete.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <view class="swipedelete-wrapper" @touchmove="touchMoveHandler" @touchstart="touchStartHandler" :style="'transform:translateX(' + translateX + 'rpx)'">
  3. <slot></slot>
  4. <view class="swipedelete-btn" @tap="deleteItem">删除</view>
  5. </view>
  6. </template>
  7. <script>
  8. let startX = 0;
  9. export default {
  10. data() {
  11. return {
  12. translateX: 0
  13. };
  14. },
  15. components: {},
  16. props: {},
  17. methods: {
  18. deleteItem: function (e) {
  19. this.translateX=0
  20. this.$emit('deleteChatItem', {}, {
  21. bubbles: true
  22. });
  23. },
  24. /**
  25. * 滑动删除事件-滑动开始
  26. */
  27. touchStartHandler: function (e) {
  28. startX = e.touches[0].pageX;
  29. },
  30. /**
  31. * 滑动删除事件-滑动
  32. */
  33. touchMoveHandler: function (e) {
  34. let pageX = e.touches[0].pageX;
  35. let moveX = pageX - startX;
  36. if (Math.abs(moveX) < 80) {
  37. return;
  38. } // e.target.style.WebkitTransform = `translateX(${moveX}px)`
  39. if (moveX > 0) {
  40. // 右滑 隐藏删除
  41. if (Math.abs(this.translateX) == 0) {
  42. return;
  43. } else {
  44. this.translateX= 0
  45. }
  46. } else {
  47. // 左滑 显示删除
  48. if (Math.abs(this.translateX) >= 160) {
  49. return;
  50. } else {
  51. this.translateX= -160
  52. }
  53. }
  54. }
  55. }
  56. };
  57. </script>
  58. <style>
  59. @import "./swipedelete.css";
  60. </style>