Vue视频背景自动播放组件

<template>
  <div class="homepage-hero-module">
    <div class="video-container">
      <div :style="fixStyle" class="filter">
        <!--内容-->
      </div>
      <video :style="fixStyle" autoplay loop muted class="fillWidth" @canplay="canplay">
        <source src="../../../../assets/video/EARTH 5 Panorama.mp4" type="video/mp4" />
        浏览器不支持 video 标签,建议升级浏览器。
        <!-- <source src="../assets/video/G1w.webm" type="video/webm" />
        浏览器不支持 video 标签,建议升级浏览器。 -->
      </video>
      <!-- <div v-if="!vedioCanPlay" class="poster hidden">
        <img :style="fixStyle" src="../assets/video/G1.jpg" alt="" />
      </div> -->
    </div>
  </div>
</template>
<script>
export default {
  props: {
    videoSource: {
      default: '../../../../assets/video/EARTH 5 Panorama.mp4',
      type: String,
    },
  },
  data() {
    return {
      vedioCanPlay: false,
      fixStyle: '',
    };
  },
  mounted() {
    this.onResizeHandler();
  },
  methods: {
    canplay() {
      this.vedioCanPlay = true;
    },
    onResizeHandler() {
      window.onresize = () => {
        const windowWidth = document.body.clientWidth;
        const windowHeight = document.body.clientHeight;
        const windowAspectRatio = windowHeight / windowWidth;
        let videoWidth;
        let videoHeight;
        if (windowAspectRatio < 0.5625) {
          videoWidth = windowWidth;
          videoHeight = videoWidth * 0.5625;
          this.fixStyle = {
            height: `${windowWidth * 0.5625}px`,
            width: `${windowWidth}px`,
            'margin-bottom': `${(windowHeight - videoHeight) / 2}px`,
            'margin-left': 'initial',
          };
        } else {
          videoHeight = windowHeight;
          videoWidth = videoHeight / 0.5625;
          this.fixStyle = {
            height: `${windowHeight}px`,
            width: `${windowHeight / 0.5625}px`,
            'margin-left': `${(windowWidth - videoWidth) / 2}px`,
            'margin-bottom': 'initial',
          };
        }
      };
    },
  },
};
</script>

<style scoped>
.homepage-hero-module,
.video-container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}

.video-container .poster img {
  z-index: 0;
  position: absolute;
}

.video-container .filter {
  z-index: 1;
  position: absolute;
  background: rgba(0, 0, 0, 0.4);
  width: 100%;
}

.fillWidth {
  width: 100%;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值