del_slideLeft.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <view>
  3. <view class="box-slideLeft" >
  4. <view class="touch-item touch-slideLeft " @touchstart="touchS" @touchmove="touchM" @touchend="touchE" :style="item_show.txtStyle">
  5. <slot />
  6. </view>
  7. <view class="touch-item del-box-touch-slideLeft cf-shuCenter" @click="delItem(item_show)">
  8. <view class="iconfont icon-shanchu"></view>
  9. </view>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. components: {
  16. },
  17. props: {
  18. data_transit: {
  19. type: Object,
  20. default () {
  21. return {}
  22. }
  23. },
  24. //可不传参
  25. item: {
  26. type: Object,
  27. default () {
  28. return {}
  29. }
  30. },
  31. },
  32. computed: {
  33. },
  34. data() {
  35. return {
  36. item_show : {},
  37. delBtnWidth: 60, //删除按钮宽度单位(rpx)
  38. startX: '',
  39. };
  40. },
  41. created:function(){
  42. //专门处理检查对象中,某字段是否存在的,如果存在返回 true 不存在返回 false
  43. let that = this ;
  44. let item = that.item ;
  45. if(!item.hasOwnProperty("txtStyle")){
  46. this.$set(this.item,'txtStyle','');//不需要初始化了
  47. }
  48. this.item_show = this.item ;
  49. },
  50. watch: {
  51. item(e){
  52. this.item_show = e ;
  53. },
  54. },
  55. methods: {
  56. //点击删除按钮事件
  57. delItem: function(e) {
  58. let that = this;
  59. let data ={
  60. item : e ,
  61. data : that.data_transit ,
  62. };
  63. this.$emit('delItem', data);
  64. var txtStyle = "";
  65. that.item_show.txtStyle = txtStyle;
  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: red;
  145. border-radius: 0 10px 10px 0;
  146. color: #fff;
  147. font-size: 18px;
  148. font-weight: lighter;
  149. text-align: center;
  150. }
  151. .icon-shanchu{
  152. font-size: 44upx;
  153. }
  154. .cf-shuCenter{
  155. display: flex;
  156. flex-direction: column;
  157. justify-content: center;
  158. align-items: center;
  159. }
  160. }
  161. </style>