15
15
<span @click.prevent="onlyDelClick($event)">删除</span></div
16
16
> -->
17
17
<div class =" nut-leftslip-item-btn" >
18
+ <!-- <a @click.prevent="onlyDelClick($event)">删除</a> -->
18
19
<slot name =" slipbtns" >
19
20
<a class =" nut-delet-btn" @click.prevent =" onlyDelClick($event)" v-if =" onlyDelBtn" >删除</a >
20
21
</slot >
@@ -33,10 +34,6 @@ export default {
33
34
btnSlipDel: {
34
35
type: Boolean ,
35
36
default: false
36
- },
37
- customBtnWidth: {
38
- type: Number ,
39
- default: 40
40
37
}
41
38
},
42
39
data () {
@@ -47,10 +44,9 @@ export default {
47
44
moveY: 0 ,
48
45
left: 0 ,
49
46
buttonWidth: 0 ,
50
- offset: 0 , // 移动距离
51
- deleteSlider: ' ' , // 滑动时的效果
52
- delBtnStyle: ' ' , // 单个删除键拖拽删除效果
53
- openState: false
47
+ disX: 0 , // 移动距离
48
+ deleteSlider: ' ' , // 滑动时的效果,使用v-bind:style="deleteSlider"
49
+ delBtnStyle: ' ' // 单个删除键拖拽删除效果
54
50
};
55
51
},
56
52
mounted () {
@@ -63,15 +59,15 @@ export default {
63
59
}
64
60
});
65
61
66
- // window.addEventListener('scroll', this.handleScroll, true);
62
+ window .addEventListener (' scroll' , this .handleScroll , true );
67
63
},
68
64
beforeDestroy () {
69
65
// 移除监听
70
- // window.removeEventListener('scroll', this.handleScroll, true);
66
+ window .removeEventListener (' scroll' , this .handleScroll , true );
71
67
},
72
68
methods: {
73
69
handleScroll () {
74
- if (this .offset ) {
70
+ if (this .disX ) {
75
71
this .restSlide ();
76
72
}
77
73
},
@@ -84,12 +80,8 @@ export default {
84
80
this .restSlide ();
85
81
},
86
82
touchStart (e ) {
87
- // if(this.openState){
88
- // return
89
- // }
90
83
this .restSlide ();
91
84
e = e || event ;
92
- // e.preventDefault();
93
85
// 等于1时表示此时有只有一只手指在触摸屏幕
94
86
if (e .touches .length == 1 ) {
95
87
this .startX = e .touches [0 ].clientX ;
@@ -98,47 +90,45 @@ export default {
98
90
},
99
91
touchMove (e ) {
100
92
e = e || event ;
101
- // e.preventDefault();
102
93
// 获取当前滑动对象
103
94
let parentElement = e .currentTarget .parentElement ;
104
-
95
+ // 获取删除按钮的宽度,此宽度为滑块左滑的最大距离
105
96
let itemWd = this .$refs .slipItem .offsetWidth ;
106
- let wd = this .onlyDelBtn ? this . customBtnWidth : this .buttonWidth ;
97
+ let wd = this .onlyDelBtn ? 40 : this .buttonWidth ;
107
98
108
99
if (e .touches .length == 1 ) {
100
+ // 滑动时距离浏览器左侧实时距离
109
101
this .moveY = e .touches [0 ].clientY ;
110
102
this .moveX = e .touches [0 ].clientX ;
111
-
112
103
if (Math .abs (this .moveY - this .startY ) < 40 ) {
113
- this .offset = this .startX - this .moveX ;
114
-
104
+ // 起始位置减去 实时的滑动的距离,得到手指实时偏移距离
105
+ this .disX = this .startX - this .moveX ;
106
+ // console.log(this.disX);
115
107
if (this .onlyDelBtn ) {
116
108
// 单一删除,左滑一键删除
117
- if (this .offset < 0 || this .offset == 0 ) {
109
+ if (this .disX < 0 || this .disX == 0 ) {
118
110
this .deleteSlider = ' transform:translateX(0px)' ;
119
- this .openState = false ;
120
111
}
121
- this .deleteSlider = ' transform:translateX(-' + this .offset + ' px)' ;
122
- this .delBtnStyle = ' width:' + this .offset + ' px' ;
112
+ this .deleteSlider = ' transform:translateX(-' + this .disX + ' px)' ;
113
+ this .delBtnStyle = ' width:' + this .disX + ' px' ;
123
114
parentElement .dataset .type = 1 ; // 设置滑动展开隐藏标志位,左滑展开为1,右滑或复位为0
124
- this .openState = true ;
125
115
} else {
126
- if (this .offset < wd / 4 || this .offset == 0 ) {
116
+ // 如果是向右滑动或者不滑动,不改变滑块的位置
117
+ if (this .disX < wd / 4 || this .disX == 0 ) {
127
118
this .deleteSlider = ' transform:translateX(0px)' ;
128
119
parentElement .dataset .type = 0 ;
129
- this .openState = false ;
130
- } else if (this .offset > wd / 4 ) {
120
+ } else if (this .disX > wd / 4 ) {
131
121
parentElement .dataset .type = 1 ;
132
- this .deleteSlider = ' transform:translateX(-' + this .offset + ' px)' ;
122
+ this .deleteSlider = ' transform:translateX(-' + this .disX + ' px)' ;
133
123
// 最大也只能等于删除按钮宽度
134
- if (this .offset >= wd) {
124
+ if (this .disX * 1.5 >= wd) {
125
+ // parentElement.dataset.type = 1;
135
126
if (wd >= itemWd) {
136
- this .deleteSlider = ' transform:translateX(-' + (itemWd - this . customBtnWidth ) + ' px)' ;
127
+ this .deleteSlider = ' transform:translateX(-' + (itemWd - 40 ) + ' px)' ;
137
128
} else {
138
129
this .deleteSlider = ' transform:translateX(-' + wd + ' px)' ;
139
130
}
140
131
}
141
- this .openState = true ;
142
132
}
143
133
}
144
134
}
@@ -148,49 +138,47 @@ export default {
148
138
e = e || event ;
149
139
let parentElement = e .currentTarget .parentElement ;
150
140
let itemWd = this .$refs .slipItem .offsetWidth ;
151
- let wd = this .onlyDelBtn ? this .customBtnWidth : this .buttonWidth ;
152
-
141
+ let wd = this .onlyDelBtn ? 40 : this .buttonWidth ;
153
142
if (e .changedTouches .length == 1 ) {
154
143
let endY = e .changedTouches [0 ].clientY ;
155
144
if (Math .abs (this .startY - endY) < 40 ) {
156
145
let endX = e .changedTouches [0 ].clientX ;
157
- this .offset = this .startX - endX;
158
-
146
+ this .disX = this .startX - endX;
147
+ // console.log('touchEndthis.disX:', this.disX);
159
148
if (this .onlyDelBtn ) {
160
149
// 单一按钮,左滑一键删除
161
- if (this .offset < 0 || this .offset == 0 ) {
150
+ if (this .disX < 0 || this .disX == 0 ) {
162
151
this .deleteSlider = ' transform:translateX(0px)' ;
163
152
parentElement .dataset .type = 0 ;
164
- this .openState = false ;
165
- } else if (this .offset < itemWd - 20 ) {
153
+ } else if (this .disX < itemWd - 20 ) {
166
154
parentElement .dataset .type = 1 ;
167
155
this .deleteSlider = ' transform:translateX(-50px);' ;
168
156
this .delBtnStyle = ' width:0px;' ;
169
- this .openState = true ;
170
157
} else {
171
158
this .deleteSlider = ' transform:translateX(-' + itemWd + ' px);' ;
172
159
this .delBtnStyle = ' width:' + itemWd + ' px;' ;
173
160
parentElement .dataset .type = 1 ;
174
- this .openState = true ;
175
161
this .onlyDelClick ();
176
162
}
177
163
} else {
178
- if (this .offset < wd / 4 ) {
164
+ // 如果距离小于删除按钮的四分之一,强行回到起点
165
+
166
+ if (this .disX < wd / 4 ) {
179
167
parentElement .dataset .type = 0 ;
180
168
this .deleteSlider = ' transform:translateX(0px)' ;
181
- this .openState = false ;
182
169
} else {
183
170
// 大于一半 滑动到最大值
184
171
parentElement .dataset .type = 1 ;
185
172
if (wd >= itemWd) {
186
173
// 按钮数不可超出整行宽度
187
- this .deleteSlider = ' transform:translateX(-' + (itemWd - this . customBtnWidth ) + ' px)' ;
174
+ this .deleteSlider = ' transform:translateX(-' + (itemWd - 40 ) + ' px)' ;
188
175
} else {
189
176
this .deleteSlider = ' transform:translateX(-' + wd + ' px)' ;
190
177
}
191
- this .openState = true ;
192
178
}
193
179
}
180
+
181
+ // console.log('touchEnd:dataset', parentElement.dataset.type);
194
182
}
195
183
}
196
184
},
0 commit comments