fix. 코드 리팩토링
This commit is contained in:
@@ -2,15 +2,85 @@
|
||||
interface Props {
|
||||
src: string
|
||||
type?: 'mp4' | 'webm'
|
||||
play?: boolean
|
||||
autoplay?: boolean
|
||||
muted?: boolean
|
||||
loop?: boolean
|
||||
playsinline?: boolean
|
||||
class?: ClassType
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
type: 'mp4',
|
||||
play: false,
|
||||
muted: true,
|
||||
loop: true,
|
||||
playsinline: true,
|
||||
})
|
||||
|
||||
const videoRef = ref<HTMLVideoElement | null>(null)
|
||||
|
||||
// autoplay prop 변경 시 재생/정지 제어
|
||||
watch(
|
||||
() => props.play,
|
||||
shouldPlay => {
|
||||
if (!videoRef.value) return
|
||||
|
||||
if (shouldPlay) {
|
||||
videoRef.value.play().catch(err => {
|
||||
console.warn('Video play failed:', err)
|
||||
})
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
videoRef.value.pause()
|
||||
videoRef.value.currentTime = 0
|
||||
}, 200)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// src 변경 시 비디오 다시 로드
|
||||
watch(
|
||||
() => props.src,
|
||||
() => {
|
||||
if (!videoRef.value) return
|
||||
|
||||
// 비디오 시간 초기화 및 새 소스 로드
|
||||
videoRef.value.currentTime = 0
|
||||
videoRef.value.load()
|
||||
|
||||
// 재생 중이었다면 다시 재생
|
||||
if (props.play) {
|
||||
nextTick(() => {
|
||||
videoRef.value?.play().catch(err => {
|
||||
console.warn('Video play failed:', err)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<video v-if="props.src" v-bind="$attrs">
|
||||
<source :src="props.src" :type="`video/${props.type}`" />
|
||||
</video>
|
||||
<div v-if="props.src" :class="['video-box', props.class]">
|
||||
<video
|
||||
ref="videoRef"
|
||||
:autoplay="props.autoplay"
|
||||
:muted="props.muted"
|
||||
:loop="props.loop"
|
||||
:playsinline="props.playsinline"
|
||||
>
|
||||
<source :src="props.src" :type="`video/${props.type}`" />
|
||||
</video>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.video-box {
|
||||
@apply overflow-hidden relative rounded-[4px] md:rounded-[8px]
|
||||
after:content-[''] after:absolute after:top-0 after:left-0 after:w-full after:h-full after:border after:border-white after:opacity-10 after:rounded-[4px] after:md:rounded-[8px];
|
||||
}
|
||||
.video-box video {
|
||||
@apply absolute top-0 left-0 w-full h-full object-cover;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user