x-slideLeft.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view>
  3. <view class="box-slideLeft">
  4. <view class="touch-item touch-slideLeft " @touchstart="touchS" @touchmove="touchM" @touchend="touchE"
  5. :style="item_show.txtStyle">
  6. <slot />
  7. </view>
  8. <view class="touch-item del-box-touch-slideLeft cf-shuCenter" @click="delItem(item_show)">
  9. <view class="delete">删除</view>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. components: {
  17. },
  18. props: {
  19. data_transit: {
  20. type: Object,
  21. default () {
  22. return {}
  23. }
  24. },
  25. //可不传参
  26. item: {
  27. type: Object,
  28. default () {
  29. return {}
  30. }
  31. },
  32. },
  33. computed: {
  34. },
  35. data() {
  36. return {
  37. item_show: {},
  38. delBtnWidth: 60, //删除按钮宽度单位(rpx)
  39. startX: '',
  40. };
  41. },
  42. created: function() {
  43. //专门处理检查对象中,某字段是否存在的,如果存在返回 true 不存在返回 false
  44. let that = this;
  45. let item = that.item;
  46. if (!item.hasOwnProperty("txtStyle")) {
  47. this.$set(this.item, 'txtStyle', ''); //不需要初始化了
  48. }
  49. this.item_show = this.item;
  50. },
  51. watch: {
  52. item(e) {
  53. this.item_show = e;
  54. },
  55. },
  56. methods: {
  57. //点击删除按钮事件
  58. delItem: function(e) {
  59. let that = this;
  60. let data = {
  61. item: e,
  62. data: that.data_transit,
  63. };
  64. console.log(data, "子组件的值");
  65. this.$emit('delItem', data);
  66. },
  67. touchS: function(e) {
  68. let that = this;
  69. if (e.touches.length == 1) {
  70. //设置触摸起始点水平方向位置
  71. this.startX = e.touches[0].clientX
  72. }
  73. },
  74. touchM: function(e) {
  75. let that = this;
  76. if (e.touches.length == 1) {
  77. //手指移动时水平方向位置
  78. var moveX = e.touches[0].clientX;
  79. //手指起始点位置与移动期间的差值
  80. var disX = this.startX - moveX;
  81. var delBtnWidth = this.delBtnWidth;
  82. var txtStyle = "";
  83. if (disX == 0 || disX < 0) { //如果移动距离小于等于0,说明向右滑动,文本层位置不变
  84. txtStyle = "left:0px";
  85. } else if (disX > 0) { //移动距离大于0,文本层left值等于手指移动距离
  86. txtStyle = "left:-" + disX + "px";
  87. if (disX >= delBtnWidth) {
  88. //控制手指移动距离最大值为删除按钮的宽度
  89. txtStyle = "left:-" + delBtnWidth + "px";
  90. }
  91. }
  92. //获取手指触摸的是哪一项
  93. that.item_show.txtStyle = txtStyle;
  94. }
  95. },
  96. touchE: function(e) {
  97. let that = this;
  98. if (e.changedTouches.length == 1) {
  99. //手指移动结束后水平位置
  100. var endX = e.changedTouches[0].clientX;
  101. //触摸开始与结束,手指移动的距离
  102. var disX = this.startX - endX;
  103. var delBtnWidth = this.delBtnWidth;
  104. //如果距离小于删除按钮的1/2,不显示删除按钮
  105. var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
  106. //获取手指触摸的是哪一项
  107. that.item_show.txtStyle = txtStyle;
  108. }
  109. },
  110. }
  111. }
  112. </script>
  113. <style lang="scss">
  114. // @import './iconfont.css';//便于有删除图标
  115. .box-slideLeft {
  116. view {
  117. box-sizing: border-box;
  118. }
  119. position: relative;
  120. overflow: hidden;
  121. .touch-item {
  122. position: absolute;
  123. top: 0;
  124. padding: 10px 10px 10px;
  125. background-color: #FFFFFF;
  126. // border-radius: 10px;
  127. overflow: hidden;
  128. }
  129. .touch-slideLeft {
  130. position: relative;
  131. width: 100%;
  132. z-index: 5;
  133. transition: left 0.2s ease-in-out;
  134. // white-space: nowrap;
  135. overflow: hidden;
  136. text-overflow: ellipsis;
  137. }
  138. .del-box-touch-slideLeft {
  139. right: 0;
  140. float: left;
  141. width: 70px;
  142. height: 100%;
  143. line-height: 101px;
  144. background-color: #FF1E1E;
  145. border-radius: 0 10px 10px 0;
  146. color: #fff;
  147. font-size: 18px;
  148. font-weight: lighter;
  149. text-align: center;
  150. }
  151. .delete {
  152. font-size: 28rpx;
  153. }
  154. .icon-shanchu {
  155. font-size: 44upx;
  156. }
  157. .cf-shuCenter {
  158. display: flex;
  159. flex-direction: column;
  160. justify-content: center;
  161. align-items: center;
  162. }
  163. }
  164. </style>