vue中动态设置source标签

项目中有个视频播放,路径通过接口返回,而且不带后缀,并不确定是什么类型的视频文件,所以要通过source标签去进行设置.

问题:当video中存在source标签的时候,浏览器渲染之后会自动去获取地址,即便地址改变,浏览器也不会再去获取地址。
解决方法1:通过v-if销毁创建元素重新获取
代码示例:

<template>
  <video controls loop v-if="showPlayer" @timeupdate="timeupdate">
    <source :src="videoSrc + '.ogg'" type="video/ogg">
    <source :src="videoSrc + '.mp4'" type="video/mp4">
    <source :src="videoSrc + '.webm'" type="video/webm">
    您的浏览器不支持 video 标签。
  </video>
</template>
<script>
export default {
  data() {
    return {
      showPlayer: false,
      videoSrc: ""
    }
  },
  mounted() {
    this.getVideo();
  },
  methods: {
    getVideo() {
     this.showPlayer=false;
      new Promise((resolve, reject) => {
        resolve('https://www.runoob.com/try/demo_source/movie')
      }).then(res => {
        this.videoSrc = res;
        this.showPlayer=true;
      })
    },
    timeupdate(){
    }
  }
}
</script>

解决方法2:用computed和v-html动态渲染,动态渲染没有ref,如果需要绑定方法也比较麻烦

<template>
  <div class="player-box" v-html="playerBox"></div>
</template>
<script>
  export default {
  data() {
    return {
      videoSrc: ""
    }
  },
  computed:{
    playerBox(){
      return `
        <video controls loop>
          <source src="${this.videoSrc}.ogg" type="video/ogg">
          <source src="${this.videoSrc}.mp4" type="video/mp4">
          <source src="${this.videoSrc}.webm" type="video/webm">
          您的浏览器不支持 video 标签。
        </video>
      `
    }
  },
  mounted() {
    this.getVideo();
  },
  methods: {
    getVideo() {
      new Promise((resolve, reject) => {
        resolve('https://www.runoob.com/try/demo_source/movie')
      }).then(res => {
        this.videoSrc = res;
      })
    }
  }
}
</script>
### 实现 Vue 中 `video` 标签全屏铺满效果的方法 为了使 `<video>` 标签Vue 应用中实现全屏铺满的效果,可以通过 CSS 和 HTML 的组合方式完成。以下是具体方法: #### 使用纯 CSS 控制视频铺满屏幕 可以利用以下 CSS 属性让视频充满整个视口: ```css .video-container { position: fixed; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden; } video { min-width: 100%; min-height: 100%; width: auto; height: auto; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } ``` 上述代码通过设置容器为固定定位,并调整子元素(即 `<video>`)的位置和大小使其覆盖整个屏幕[^1]。 #### 结合 Vue 组件结构 在 Vue 中,HTML 可能看起来像这样: ```html <template> <div class="video-container"> <video autoplay muted loop> <source src="/path/to/your/video.mp4" type="video/mp4"> Your browser does not support the video tag. </video> </div> </template> <script> export default { name: "FullScreenVideo" }; </script> <style scoped> /* 插入上面提到的CSS */ </style> ``` 此模板定义了一个名为 `FullScreenVideo` 的组件,在其中嵌套了带有自适应样式的 `<video>` 元素[^2]。 #### 替代方案:使用外部库 (如 vue-video-player) 对于更复杂的场景或者需要额外功能的情况,推荐使用第三方插件比如 `vue-video-player` 来简化开发过程。按照官方文档说明安装依赖项之后,可以在项目入口文件 main.js 添加初始化脚本: ```javascript import Video from 'video.js'; import 'video.js/dist/video-js.css'; Vue.prototype.$video = Video; ``` 接着创建一个新的 Vue 组件用于展示视频内容: ```html <template> <video-player :options="playerOptions"></video-player> </template> <script> export default { data() { return { playerOptions: { sources: [{ src: '/path/to/your/video.mp4', type: 'video/mp4' }], autoplay: true, controls: false, fluid: true // 自动缩放至父级容器尺寸 } }; }, }; </script> ``` 这里的关键参数之一就是 `fluid`, 它允许播放器动态适配其所在区域的大校[^3] #### 处理特殊情况下的兼容性问题 某些情况下单纯依靠 CSS 或者现有框架可能难以达到理想中的视觉效果,这时就需要考虑其他变通手段。例如当海报图(Poster)无法正常加载时,可以用包裹层的方式模拟背景图像的行为[^4]: ```html <div style="position:relative;width:100%;height:100%"> <!-- Background Image --> <img src="/path/to/poster.jpg" alt="" usemap="#image-map" style="width:100%;height:auto;object-fit:cover;"> <!-- Overlayed Video --> <video autoplay muted loop playsinline style="position:absolute;top:0;left:0;width:100%;height:100%;"> <source src="/path/to/your/video.mp4" type="video/mp4"> </video> </div> ``` 以上介绍了几种常见的技术路径帮助开发者实现在 Vue 环境下将视频作为网页背景并确保它能够完全填充浏览器窗口的技术细节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值