3
3
<view
4
4
:style =" imageStyle"
5
5
class =" nut-imagepreview-box"
6
- v-if =" image && image.src"
7
6
@touchstart =" onTouchStart"
8
7
@touchmove =" onTouchMove"
9
8
@touchend =" onTouchEnd"
10
9
@touchcancel =" onTouchEnd"
11
10
>
12
- <img :src =" image.src" class =" nut-imagepreview-img" @load =" imageLoad" />
13
- </view >
14
-
15
- <view
16
- class =" nut-imagepreview-box"
17
- v-if =" video && video.source"
18
- @click =" videoClick"
19
- @touchstart =" onTouchStart"
20
- @touchmove =" onTouchMove"
21
- @touchend =" onTouchEnd"
22
- @touchcancel =" onTouchEnd"
23
- >
24
- <nut-video :source =" video.source" :options =" video.options" ></nut-video >
11
+ <img v-if =" image && image.src" :src =" image.src" class =" nut-imagepreview-img" @load =" imageLoad" />
12
+ <nut-video v-if =" video && video.source" :source =" video.source" :options =" video.options" ></nut-video >
25
13
</view >
26
14
</nut-swiper-item >
27
15
</template >
28
16
<script lang="ts">
29
17
import { toRefs , reactive , watch , computed , CSSProperties , PropType } from ' vue' ;
30
18
import { createComponent } from ' @/packages/utils/create' ;
31
- import Popup from ' ../popup/index.vue' ;
32
- import Video from ' ../video/index.vue' ;
33
- import Swiper from ' ../swiper/index.vue' ;
34
- import SwiperItem from ' ../swiperitem/index.vue' ;
35
- import Icon from ' ../icon/index.vue' ;
36
19
import { useTouch } from ' @/packages/utils/useTouch' ;
20
+ import { preventDefault } from ' @/packages/utils/util' ;
37
21
import { ImageInterface } from ' ./types' ;
22
+ import { baseProps } from ' ./types' ;
38
23
const { create } = createComponent (' imagepreviewitem' );
39
24
40
25
export default create ({
41
26
props: {
42
- show: {
43
- type: Boolean ,
44
- default: false
45
- },
46
- initNo: Number ,
27
+ ... baseProps ,
47
28
image: {
48
29
type: Object as PropType <ImageInterface >,
49
30
default : () => ({})
@@ -52,36 +33,17 @@ export default create({
52
33
type: Object ,
53
34
default : () => ({})
54
35
},
55
-
56
- showIndex: {
57
- type: Boolean ,
58
- default: true
59
- },
60
36
rootWidth: {
61
37
type: Number ,
62
38
default: 0
63
39
},
64
40
rootHeight: {
65
41
type: Number ,
66
42
default: 0
67
- },
68
- minZoom: {
69
- type: Number ,
70
- default: 1 / 3
71
- },
72
- maxZoom: {
73
- type: Number ,
74
- default: 3
75
43
}
76
44
},
77
45
emits: [' close' , ' scale' ],
78
- components: {
79
- [Popup .name ]: Popup ,
80
- [Video .name ]: Video ,
81
- [Swiper .name ]: Swiper ,
82
- [SwiperItem .name ]: SwiperItem ,
83
- [Icon .name ]: Icon
84
- },
46
+ components: {},
85
47
86
48
setup(props , { emit }) {
87
49
const state = reactive ({
@@ -103,20 +65,25 @@ export default create({
103
65
return state .imageRatio > rootRatio ;
104
66
});
105
67
106
- // 图片放大
68
+ // 图片缩放
107
69
const imageStyle = computed (() => {
108
- const { scale, moveX, moveY, moving, zooming } = state ;
109
- const style: CSSProperties = {
110
- transitionDuration: zooming || moving ? ' 0s' : ' .3s'
111
- };
112
-
113
- if (scale !== 1 ) {
114
- const offsetX = moveX / scale ;
115
- const offsetY = moveY / scale ;
116
- style .transform = ` scale(${scale }, ${scale }) translate(${offsetX }px, ${offsetY }px) ` ;
70
+ const images = props .image ;
71
+ if (images && images .src ) {
72
+ const { scale, moveX, moveY, moving, zooming } = state ;
73
+ const style: CSSProperties = {
74
+ transitionDuration: zooming || moving ? ' 0s' : ' .3s'
75
+ };
76
+
77
+ if (scale !== 1 ) {
78
+ const offsetX = moveX / scale ;
79
+ const offsetY = moveY / scale ;
80
+ style .transform = ` scale(${scale }, ${scale }) translate(${offsetX }px, ${offsetY }px) ` ;
81
+ }
82
+
83
+ return style ;
117
84
}
118
85
119
- return style ;
86
+ return {} ;
120
87
});
121
88
122
89
const maxMoveX = computed (() => {
@@ -258,10 +225,8 @@ export default create({
258
225
};
259
226
260
227
const onTouchEnd = (event : TouchEvent ) => {
261
- console .log (' ontauchend' );
262
228
let stopPropagation = false ;
263
229
264
- /* istanbul ignore else */
265
230
if (state .moving || state .zooming ) {
266
231
stopPropagation = true ;
267
232
@@ -291,30 +256,15 @@ export default create({
291
256
}
292
257
}
293
258
294
- // eliminate tap delay on safari
295
259
preventDefault (event , stopPropagation );
296
260
297
261
checkTap ();
298
262
touch .reset ();
299
263
};
300
264
301
- // 阻止
302
- const preventDefault = (event : Event , isStopPropagation ? : boolean ) => {
303
- if (typeof event .cancelable !== ' boolean' || event .cancelable ) {
304
- event .preventDefault ();
305
- }
306
-
307
- if (isStopPropagation ) {
308
- event .stopPropagation ();
309
- }
310
- };
311
-
312
265
const clamp = (num : number , min : number , max : number ): number => Math .min (Math .max (num , min ), max );
313
266
314
- // 视频点击
315
- const closeSwiper = (event : any ) => {
316
- console .log (' 关闭视频' );
317
- // event.preventDefault();
267
+ const closeSwiper = () => {
318
268
emit (' close' );
319
269
};
320
270
0 commit comments