VideoPlayer.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <div>
  3. <div v-if="videoPauseShow" class="video-mask" @click="handlePlayVideo">
  4. <el-icon class="video-mask-play" :size="iconSize"><VideoPlay /></el-icon>
  5. </div>
  6. <video
  7. ref="videoRef"
  8. :key="videoUrl"
  9. type="video/mp4"
  10. :muted="muted"
  11. :preload="preload"
  12. :controls="controls"
  13. disablepictureinpicture
  14. :style="{ objectFit: 'contain', width: '100%', height: videoHeight }"
  15. @pause="handlePauseVideo"
  16. @play="handlePlayVideo"
  17. >
  18. <source :src="videoUrl" />
  19. </video>
  20. </div>
  21. </template>
  22. <script setup>
  23. import { ref } from 'vue';
  24. import { ElIcon } from 'element-plus';
  25. import { VideoPlay } from '@element-plus/icons-vue';
  26. const props = defineProps({
  27. videoUrl: {
  28. type: String,
  29. required: true,
  30. },
  31. iconSize: {
  32. type: Number,
  33. default: 140,
  34. },
  35. controls: {
  36. type: Boolean,
  37. default: true,
  38. },
  39. muted: {
  40. type: Boolean,
  41. default: true,
  42. },
  43. preload: {
  44. type: String,
  45. default: 'auto',
  46. },
  47. videoHeight: {
  48. type: String,
  49. default: '432px',
  50. },
  51. });
  52. const videoRef = ref(null);
  53. const videoPauseShow = ref(true);
  54. const handlePlayVideo = () => {
  55. videoPauseShow.value = false;
  56. if (videoRef.value) {
  57. videoRef.value.play();
  58. }
  59. };
  60. const handlePauseVideo = () => {
  61. videoPauseShow.value = true;
  62. if (videoRef.value) {
  63. videoRef.value.pause();
  64. }
  65. };
  66. </script>
  67. <style scoped>
  68. .video-mask {
  69. position: absolute;
  70. top: 0;
  71. left: 0;
  72. width: 100%;
  73. height: 100%;
  74. background-color: rgba(0, 0, 0, 0.5);
  75. display: flex;
  76. align-items: center;
  77. justify-content: center;
  78. color: #fff;
  79. z-index: 1;
  80. }
  81. .video-mask-play {
  82. cursor: pointer;
  83. }
  84. .video-mask-play:hover {
  85. animation: warn 1.5s ease-out infinite;
  86. }
  87. @keyframes warn {
  88. 0% {
  89. transform: scale(1);
  90. opacity: 1;
  91. }
  92. 100% {
  93. transform: scale(1.4);
  94. opacity: 0;
  95. }
  96. }
  97. </style>