OpeningAnimation.vue 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <script lang="ts" setup>
  2. import {onMounted, ref, watch} from "vue";
  3. const openingAnimationSrc = ref(new URL('@/assets/video/openingAnimation.mp4', import.meta.url).href)
  4. const openingAnimationRef = ref(null)
  5. const isPlaying = ref(false);
  6. const emit = defineEmits(['close']);
  7. const onPlay = () => {
  8. isPlaying.value = true;
  9. };
  10. const onPause = () => {
  11. isPlaying.value = false;
  12. };
  13. const onEnded = () => {
  14. isPlaying.value = false;
  15. emit('close')
  16. };
  17. // 监听 isPlaying 的变化
  18. watch(isPlaying, (newVal) => {
  19. console.log(`Video is ${newVal ? 'playing' : 'paused'}`);
  20. });
  21. onMounted(() => {
  22. if (openingAnimationRef.value) {
  23. // 初始状态设置
  24. isPlaying.value = !openingAnimationRef.value.paused;
  25. }
  26. });
  27. </script>
  28. <template>
  29. <video ref="openingAnimationRef" autoplay class="opening-animation-video" muted @ended="onEnded" @pause="onPause"
  30. @play="onPlay">
  31. <source :src="openingAnimationSrc" type="video/mp4">
  32. </video>
  33. </template>
  34. <style lang="scss" scoped>
  35. .opening-animation-video {
  36. width: 100%;
  37. height: 100%;
  38. z-index: 1;
  39. position: fixed;
  40. object-fit: cover;
  41. }
  42. </style>