1
1
<template >
2
- <nut-popup pop-class =" nut-imagepreview-custom-pop" v-model:visible =" showPop" @click =" onClose" style =" width : 100% " >
3
- <!-- @click.stop="closeOnImg" -->
2
+ <nut-popup pop-class =" nut-imagepreview-custom-pop" v-model:visible =" showPop" @closed =" onClose" >
4
3
<view class =" nut-imagepreview" @touchstart.capture =" onTouchStart" >
5
4
<nut-swiper
6
5
v-if =" showPop"
9
8
:loop =" isLoop"
10
9
:is-preventDefault =" false"
11
10
direction =" horizontal"
12
- @change =" slideChangeEnd "
13
- :init-page =" initPage "
11
+ @change =" setActive "
12
+ :init-page =" initNo "
14
13
:pagination-visible =" paginationVisible"
15
14
:pagination-color =" paginationColor"
16
15
>
17
- <nut-swiper-item v-for =" (item, index) in images" :key =" index" >
16
+ <nut-swiper-item v-for =" (item, index) in images" :key =" index" @click = " onClose " >
18
17
<image mode =" aspectFit" :src =" item.src" class =" nut-imagepreview-taro-img" v-if =" ENV != ENV_TYPE.WEB" />
19
18
<img :src =" item.src" mode =" aspectFit" class =" nut-imagepreview-img" v-else />
20
19
</nut-swiper-item >
21
20
</nut-swiper >
22
21
</view >
23
22
24
- <view class =" nut-imagepreview-index" v-if =" showIndex" > {{ active }} / {{ images.length }} </view >
25
- <view class =" nut-imagepreview-close-icon" @click =" handleCloseIcon " :style =" styles" v-if =" closeable"
23
+ <view class =" nut-imagepreview-index" v-if =" showIndex" > {{ active + 1 }} / {{ images.length }} </view >
24
+ <view class =" nut-imagepreview-close-icon" @click =" onClose " :style =" styles" v-if =" closeable"
26
25
><nut-icon :name =" closeIcon" v-bind =" $attrs" color =" #ffffff" ></nut-icon
27
26
></view >
28
27
</nut-popup >
29
28
</template >
30
29
<script lang="ts">
31
30
import { toRefs , reactive , watch , onMounted , computed , CSSProperties , PropType } from ' vue' ;
32
31
import { createComponent } from ' @/packages/utils/create' ;
33
- import Popup from ' ../popup/index.taro.vue' ;
34
- import Swiper from ' ../swiper/index.taro.vue' ;
35
- import SwiperItem from ' ../swiperitem/index.taro.vue' ;
36
- import Icon from ' ../icon/index.taro.vue' ;
37
- import { isPromise } from ' @/packages/utils/util' ;
32
+ import { funInterceptor , Interceptor } from ' @/packages/utils/util' ;
38
33
import { ImageInterface } from ' ./types' ;
39
34
import Taro from ' @tarojs/taro' ;
40
35
const { create } = createComponent (' imagepreview' );
@@ -55,7 +50,7 @@ export default create({
55
50
},
56
51
initNo: {
57
52
type: Number ,
58
- default: 1
53
+ default: 0
59
54
},
60
55
paginationVisible: {
61
56
type: Boolean ,
@@ -85,29 +80,19 @@ export default create({
85
80
type: String ,
86
81
default: ' top-right' // top-right top-left
87
82
},
88
- beforeClose: Function ,
83
+ beforeClose: Function as PropType < Interceptor > ,
89
84
isLoop: {
90
85
type: Boolean ,
91
86
default: true
92
87
}
93
88
},
94
89
emits: [' close' , ' change' ],
95
- components: {
96
- [Popup .name ]: Popup ,
97
- // [Video.name]: Video,
98
- [Swiper .name ]: Swiper ,
99
- [SwiperItem .name ]: SwiperItem ,
100
- [Icon .name ]: Icon
101
- },
90
+ components: {},
102
91
103
92
setup(props , { emit }) {
104
93
const state = reactive ({
105
94
showPop: false ,
106
- active: 1 ,
107
- source: {
108
- src: ' https://storage.jd.com/about/big-final.mp4?Expires=3730193075&AccessKey=3LoYX1dQWa6ZXzQl&Signature=ViMFjz%2BOkBxS%2FY1rjtUVqbopbJI%3D' ,
109
- type: ' video/mp4'
110
- },
95
+ active: 0 ,
111
96
options: {
112
97
muted: true ,
113
98
controls: true
@@ -135,9 +120,12 @@ export default create({
135
120
return style ;
136
121
});
137
122
138
- const slideChangeEnd = function (page : number ) {
139
- state .active = page + 1 ;
140
- emit (' change' , state .active );
123
+ // 设置当前选中第几个
124
+ const setActive = (active : number ) => {
125
+ if (active !== state .active ) {
126
+ state .active = active ;
127
+ emit (' change' , state .active );
128
+ }
141
129
};
142
130
143
131
const closeOnImg = () => {
@@ -148,35 +136,21 @@ export default create({
148
136
};
149
137
150
138
const onClose = () => {
151
- if (props .beforeClose ) {
152
- const returnVal = props .beforeClose .apply (null , state .active );
153
-
154
- if (isPromise (returnVal )) {
155
- returnVal .then ((value ) => {
156
- if (value ) {
157
- closeDone ();
158
- }
159
- });
160
- } else if (returnVal ) {
161
- closeDone ();
162
- }
163
- } else {
164
- closeDone ();
165
- }
139
+ funInterceptor (props .beforeClose , {
140
+ args: [state .active ],
141
+ done : () => closeDone ()
142
+ });
166
143
};
167
144
// 执行关闭
168
145
const closeDone = () => {
169
146
state .showPop = false ;
170
147
state .store .scale = 1 ;
171
148
scaleNow ();
172
- // state.active = 1;
173
149
emit (' close' );
174
150
};
175
151
176
152
// 计算两个点的距离
177
153
const getDistance = (first : { x: number ; y: number }, second : { x: number ; y: number }) => {
178
- // 计算两个点起始时刻的距离和终止时刻的距离,终止时刻距离变大了则放大,变小了则缩小
179
- // 放大 k 倍则 scale 也 扩大 k 倍
180
154
return Math .hypot (Math .abs (second .x - first .x ), Math .abs (second .y - first .y ));
181
155
};
182
156
@@ -187,8 +161,6 @@ export default create({
187
161
};
188
162
189
163
const onTouchStart = (event : TouchEvent ) => {
190
- // console.log('start');
191
- // 如果已经放大,双击应变回原尺寸;如果是原尺寸,双击应放大
192
164
const curTouchTime = new Date ().getTime ();
193
165
if (curTouchTime - state .lastTouchEndTime < 300 ) {
194
166
const store = state .store ;
@@ -204,13 +176,10 @@ export default create({
204
176
var events = touches [0 ];
205
177
var events2 = touches [1 ];
206
178
207
- // event.preventDefault();
208
-
209
179
const store = state .store ;
210
180
store .moveable = true ;
211
181
212
182
if (events2 ) {
213
- // 如果开始两指操作,记录初始时刻两指间的距离
214
183
store .oriDistance = getDistance (
215
184
{
216
185
x: events .pageX ,
@@ -222,7 +191,7 @@ export default create({
222
191
}
223
192
);
224
193
}
225
- // 取到开始两指操作时的放大(缩小比例),store.scale 存储的是当前的放缩比(相对于标准大小 scale 为 1 的情况的放大缩小比)
194
+
226
195
store .originScale = store .scale || 1 ;
227
196
};
228
197
@@ -265,7 +234,6 @@ export default create({
265
234
};
266
235
267
236
const onTouchEnd = () => {
268
- // console.log('end');
269
237
state .lastTouchEndTime = new Date ().getTime ();
270
238
const store = state .store ;
271
239
store .moveable = false ;
@@ -293,42 +261,25 @@ export default create({
293
261
watch (
294
262
() => props .initNo ,
295
263
(val ) => {
296
- if (val != state .active ) {
297
- state .active = val ;
298
- }
264
+ if (val != state .active ) setActive (val );
299
265
}
300
266
);
301
267
302
- const initPage = computed (() => {
303
- const maxNo = props .images .length ;
304
- const _initPage = props .initNo > maxNo ? maxNo - 1 : props .initNo - 1 ;
305
- return _initPage >= 0 ? _initPage : 0 ;
306
- });
307
-
308
- // 点击关闭按钮
309
- const handleCloseIcon = () => {
310
- onClose ();
311
- };
312
-
313
268
onMounted (() => {
314
- // 初始化页码
315
- state .active = props .initNo ;
316
- state .showPop = props .show ;
269
+ setActive (props .initNo );
317
270
});
318
271
319
272
return {
320
273
... toRefs (state ),
321
- initPage ,
322
- slideChangeEnd ,
274
+ setActive ,
323
275
onClose ,
324
276
closeOnImg ,
325
277
onTouchStart ,
326
278
onTouchMove ,
327
279
onTouchEnd ,
328
280
getDistance ,
329
281
scaleNow ,
330
- styles ,
331
- handleCloseIcon
282
+ styles
332
283
};
333
284
}
334
285
});
0 commit comments