index.vue
4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<template>
<div class="banner-box" ref="root">
<div class="wrapper">
<div v-for="(item, index) in option.dataset" :key="index + item" :class="item.className" :style="item.sty">
<CameraItem
ref="cameraRef"
:name="item.name"
:avatar="item.avatar"
:key="item + index"
:sourceSrc="item.url"
:w="w"
:h="h"
:index="index"
/>
<span class="video-title">{{ item.name }}</span>
</div>
</div>
<a href="javascript:;" class="left" @click="changeSlide('left')"></a>
<a href="javascript:;" class="right" @click="changeSlide('right')"></a>
</div>
</template>
<script setup lang="ts">
import { PropType, watch, toRefs, shallowReactive, onMounted, ref } from 'vue'
import { CreateComponentType } from '@/packages/index.d'
import 'video.js/dist/video-js.min.css'
import { option as configOption } from './config'
import CameraItem from './cameraItem.vue'
const props = defineProps({
chartConfig: {
type: Object as PropType<CreateComponentType>,
required: true
}
})
const { w, h } = toRefs(props.chartConfig.attr)
const option = shallowReactive({
dataset: configOption.dataset
})
const cameraRef = ref<InstanceType<typeof CameraItem>>()
let initial = ref(0)
let interval = ref(2500)
const computedFunc = (initial: number, source: any) => {
if (initial < 0) initial = 0
if (Array.isArray(source)) {
let len = source.length,
temp1 = initial - 2 < 0 ? initial - 2 + len : initial - 2,
temp2 = initial - 1 < 0 ? initial - 1 + len : initial - 1,
temp3 = initial,
temp4 = initial + 1 >= len ? initial + 1 - len : initial + 1,
temp5 = initial + 2 >= len ? initial + 2 - len : initial + 2
return source?.map((item: any, index: number) => {
let transform = `translateX(-50%) scale(0.7)`,
zIndex = 0,
className = 'slide'
switch (index) {
case temp3:
transform = `translateX(-50%) scale(1)`
className = ['slide', 'activate'] as any
zIndex = 300
break
case temp1:
transform = `translateX(-80%) scale(0.7)`
zIndex = 100
break
case temp5:
transform = `translateX(100%) scale(0.7)`
zIndex = 100
break
case temp2:
transform = `translateX(-100%) scale(0.85)`
zIndex = 200
break
case temp4:
transform = `translateX(58%) scale(0.85)`
zIndex = 200
break
}
item.sty = {
transform,
zIndex,
}
item.className = className
return item
})
}
}
watch(
() => props.chartConfig.option.dataset,
newData => {
option.dataset = newData
},
{
immediate: true,
deep: true
}
)
option.dataset = computedFunc(initial.value, option.dataset)
watch(
() => initial.value,
newV => {
option.dataset = computedFunc(newV, option.dataset)
}
)
// 处理自动轮播
let timer: any = null
const autoPlay = () => {
timer = setInterval(() => {
initial.value++
if (initial.value >= option.dataset.length) {
initial.value = 0
}
}, interval.value)
}
// 鼠标移入移除效果
let root = ref(null)
onMounted(() => {
clearInterval(timer)
autoPlay()
const box: any = root.value
box.onmouseenter = () => clearInterval(timer)
box.onmouseleave = () => autoPlay()
})
// 点击左右按钮切换图片
function changeVideo(dir: string) {
if (dir === 'left') {
// cameraRef.value?.handleVideoPlay()
clearInterval(timer)
initial.value++
initial.value >= option.dataset.length ? (initial.value = 0) : false
return
}
// cameraRef.value?.handleVideoDispose()
initial.value--
initial.value < 0 ? (initial.value = option.dataset.length - 1) : false
}
// 左右切换图片设置防抖效果
function changeSlide(dir: string) {
changeVideo(dir)
}
</script>
<style lang="scss" scoped>
.banner-box {
.wrapper {
// width: 100%;
height: 100%;
// position: relative;
display: flex;
overflow: hidden;
.slide {
width: 20%;
height: 100%;
position: absolute;
// top: 50%;
left: 10%;
transform: translateX(-50%);
transition: 0.5s;
box-shadow: 0 0 4px black;
.video-title {
width: v-bind('w+"px"');
font-size: 30px;
color: white;
position: absolute;
bottom: 6%;
left: 10%;
z-index: 999;
}
}
}
.arrow {
position: absolute;
top: 50%;
transform: translateY(-50%);
z-index: 9;
width: 50px;
height: 50px;
background-size: contain;
background-color: white;
opacity: 0.5;
}
a.left {
@extend .arrow;
background-image: url(./left.svg);
left: 0px;
}
a.right {
@extend .arrow;
background-image: url(./right.svg);
right: 0px;
}
}
</style>