爱学习的啊豪-前端 2020-02-15 20:17 采纳率: 0%
浏览 358

vuecli 如何让一个元素自适应 现已经监听到窗口的宽度 就差设置某个元素的高了 有哪个大佬帮我下呗 百度我搜不到了!还有就是video-player动态切换视频源怎么整?小白提问

这是做自适应的代码这个是准备做动态播放视频源

  • 写回答

1条回答 默认 最新

  • lshen01 2023-03-17 09:06
    关注

    参考GPT和自己的思路:

    关于Vue CLI如何让一个元素自适应宽度和高度,可以使用CSS的widthheight属性设置为百分比,如:width: 100%; height: 100%;,这样该元素就可以根据容器的大小自适应,但需要注意该元素的父元素也必须设置为自适应大小。

    至于你的代码中需要设置某个元素的高度,可以使用Vue.js中的ref来获取该元素的DOM,然后通过修改DOM元素的样式来动态改变高度。示例代码如下:

    Template:

    <template>
      <div ref="myElem">
        ...
      </div>
    </template>
    

    Script:

    <script>
    export default {
      name: 'MyComponent',
      mounted() {
        window.addEventListener('resize', this.handleResize);
        this.handleResize();
      },
      methods: {
        handleResize() {
          const elem = this.$refs.myElem;
          if (elem) {
            // 获取容器的宽度
            const width = elem.offsetWidth;
            // 设置高度为宽度的一半
            elem.style.height = `${width / 2}px`;
          }
        },
      },
    };
    </script>
    

    关于video-player动态切换视频源的问题,可以通过修改source标签的src属性来实现。示例代码如下:

    Template:

    <template>
      <div>
        <video ref="videoPlayer" controls>
          <source :src="currentSource" type="video/mp4" />
        </video>
        <button @click="handleSwitchSource">Switch Source</button>
      </div>
    </template>
    

    Script:

    <script>
    export default {
      name: 'MyComponent',
      data() {
        return {
          sources: [
            'https://example.com/video1.mp4',
            'https://example.com/video2.mp4',
          ],
          currentSourceIndex: 0,
        };
      },
      computed: {
        currentSource() {
          return this.sources[this.currentSourceIndex];
        },
      },
      methods: {
        handleSwitchSource() {
          this.currentSourceIndex = (this.currentSourceIndex + 1) % this.sources.length;
          const videoElem = this.$refs.videoPlayer;
          if (videoElem) {
            videoElem.pause();
            videoElem.load();
            videoElem.play();
          }
        },
      },
    };
    </script>
    

    以上是简单的解决方案,如果还有问题可以继续提问。

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能