【问题标题】:Angular 2+ iframe video player click eventAngular 2+ iframe 视频播放器点击事件
【发布时间】:2021-06-21 02:37:15
【问题描述】:

我有这个在加载时播放视频的 iframe:

<section>
  <div class="row" >
    <div style="padding-top: 56.25%">
      <iframe
        src="https://players.brightcove.net...&autoplay&muted&allowfullscreen&webkitallowfullscreen&loop&fullscreen"
        allowfullscreen
        webkitallowfullscreen
        mozallowfullscreen
        allow="encrypted-media"
        style="
          position: absolute;
          top: 0px;
          right: 0px;
          bottom: 0px;
          left: 0px;
          width: 100%;
          height: 100%;
        "
      ></iframe>
    </div>
  </div>
</section>

我想在视频发生点击事件后运行一个函数。基本上我想在有人点击视频时离开这个组件(这个视频作为屏幕保护程序的一部分播放)。如何正确编写组件?

问题在于,由于 iframe 中正在播放视频,因此外部 div 上的任何事件都不起作用。点击后它只是暂停/播放视频。

【问题讨论】:

    标签: angular typescript iframe html5-video


    【解决方案1】:

    解决方案:在其顶部覆盖一个 div 来处理 onclick 事件

    <section>
      <div class="row" >
        <div style="padding-top: 56.25%">
          <iframe
      src="https://players.brightcove.net...&autoplay&muted&allowfullscreen&webkitallowfullscreen&loop&fullscreen"
            allowfullscreen
            webkitallowfullscreen
            mozallowfullscreen
            allow="encrypted-media"
            style="
              position: absolute;
              top: 0px;
              right: 0px;
              bottom: 0px;
              left: 0px;
              width: 100%;
              height: 100%;
            "
          ></iframe>
          <div style="
              z-index: 1;
              position: absolute;
              top: 0px;
              right: 0px;
              bottom: 0px;
              left: 0px;
              width: 100%;
              height: 100%;
            "
            (click)="doTheThing()"></div>
        </div>
      </div>
    </section>
    

    谢天谢地,你是全屏的,否则确保覆盖在顶部会很痛苦。如果源也由您提供,您可以直接访问 iframe 的 contentDocument.body 并向其中添加事件侦听器。

    【讨论】:

    • 您好唐纳德,感谢您的回复。还有其他方法可以解决这个问题吗?视频上有“全屏”切换,用户可能需要点击它。这样整个页面(和视频控件)就无法点击了。
    • @EfronA。您需要检测您是否处于全屏模式,然后使用 IF/ELSE 更改覆盖的深度 z-index(eg: -1 的值将其置于页面上其他所有内容下的底层)根据需要。
    猜你喜欢
    • 2013-03-03
    • 2012-11-17
    • 1970-01-01
    • 2021-02-21
    • 2017-07-31
    • 1970-01-01
    • 2016-12-16
    • 2021-08-03
    • 2019-08-13
    相关资源
    最近更新 更多