1
1
<script setup lang="ts">
2
- import { NCarousel } from ' naive-ui' ;
3
- import { type PropType } from ' vue' ;
2
+ import { type CarouselInst , NCarousel } from ' naive-ui' ;
3
+ import { type PropType , ref } from ' vue' ;
4
4
5
5
import type { YoutubePlayerProps } from ' ~/models/youtube-player.model' ;
6
6
7
7
import YoutubePlayer from ' ~/components/common/video/YoutubePlayer.vue' ;
8
+ import { handleSwipeLeftRight , SwipeDirection } from ' ~/utils/touch.utils' ;
8
9
9
10
defineProps ({
10
11
trailers: {
@@ -17,18 +18,46 @@ defineProps({
17
18
default: false ,
18
19
},
19
20
});
21
+
22
+ const carouselRef = ref <CarouselInst & InstanceType <typeof NCarousel >>();
23
+ const touchStart = ref <TouchEvent >();
24
+
25
+ const onTouchStart = (e : TouchEvent ) => {
26
+ touchStart .value = e ;
27
+ };
28
+
29
+ const onTouchEnd = (e : TouchEvent ) => {
30
+ const _touchStart = touchStart .value ?.targetTouches ?.[0 ];
31
+ const _touchEnd = e .changedTouches ?.[0 ];
32
+ if (! _touchStart ) return ;
33
+ touchStart .value = undefined ;
34
+ const _ref = carouselRef .value ;
35
+ if (! _ref ) return ;
36
+ const { clientWidth, clientHeight } = _ref .$el || {};
37
+ const swipe = handleSwipeLeftRight (_touchStart , _touchEnd , {
38
+ vertical: clientHeight ? Math .min (clientHeight / 2 , 200 ) : 200 ,
39
+ left: clientWidth ? Math .min (clientWidth / 2 , 100 ) : 100 ,
40
+ right: clientWidth ? Math .min (clientWidth / 2 , 100 ) : 100 ,
41
+ });
42
+ if (! swipe ) return ;
43
+ if (swipe === SwipeDirection .Right ) _ref .next ();
44
+ if (swipe === SwipeDirection .Left ) _ref .prev ();
45
+ };
20
46
</script >
21
47
22
48
<template >
23
49
<NCarousel
24
50
v-if =" !disabled && trailers?.length"
51
+ ref =" carouselRef"
25
52
class =" carousel"
26
53
dot-type =" line"
27
54
effect =" slide"
28
55
centered-slides
29
56
keyboard
30
57
trigger =" hover"
31
58
:space-between =" 32"
59
+ @touchstart =" onTouchStart"
60
+ @touchend =" onTouchEnd"
32
61
>
33
62
<slot />
34
63
<YoutubePlayer
0 commit comments