多个iframe触发play()。

huangapple go评论76阅读模式
英文:

Multiple Iframe trigger play()

问题

我有多个页面上的iframe Vimeo视频。每个视频都有一个带有播放图标的图像封面,用于隐藏默认播放器。当用户点击封面时,封面会淡出并播放视频。

我的代码只在页面上有一个iframe时才能工作。如果页面上有两个或更多个iframe,则会失败。

iframe的HTML:

<div class="o-iframe">
    <div class="o-iframe__wrapper">
        <div class="o-iframe__cover js-iframe-cover">
            <div class="o-iframe__cover-icon"></div>
        </div>
        <iframe class="o-iframe__element js-iframe" width="560" height="315" src="" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
    </div>
</div>

JS代码:

if (document.querySelector('.js-iframe')) {

    var video = new Vimeo.Player(document.querySelector('.js-iframe'));
    var videoCover = document.querySelector('.js-iframe-cover');

    videoCover.addEventListener('click', function() {
        videoCover.style.opacity = '0';
        video.play();
    });

    videoCover.addEventListener('transitionend', function() {
        this.remove();
    });
}

谢谢。

英文:

I've multiple iframe Vimeo videos on the same page. Each video has an image cover with a play icon to hide the default player. When the user click the cover, the cover fade out and play the video.

My code works only if there is one iframe per page. It fails if there is two or more iframe per page.

The HTML for the iframe:

&lt;div class=&quot;o-iframe&quot;&gt;
	&lt;div class=&quot;o-iframe__wrapper&quot;&gt;
		&lt;div class=&quot;o-iframe__cover js-iframe-cover&quot;&gt;
			&lt;div class=&quot;o-iframe__cover-icon&quot;&gt;&lt;/div&gt;
		&lt;/div&gt;
		&lt;iframe class=&quot;o-iframe__element js-iframe&quot; width=&quot;560&quot; height=&quot;315&quot; src=&quot;&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;
	&lt;/div&gt;
&lt;/div&gt;

The JS:

if (document.querySelector(&#39;.js-iframe&#39;)) {

	var video = new Vimeo.Player(document.querySelector(&#39;.js-iframe&#39;));
	var videoCover = document.querySelector(&#39;.js-iframe-cover&#39;);

	videoCover.addEventListener(&#39;click&#39;, function() {
		videoCover.style.opacity = &#39;0&#39;;
		video.play();
	});

	videoCover.addEventListener(&#39;transitionend&#39;, function() {
		this.remove();
	});
}

Thank you.

答案1

得分: 1

I'll provide the translation for the code portion as requested:

querySelectorAll  ***forEach*** 是关键在页面上选择所有 `class=&quot;o-iframe&quot;` 元素在每个函数内部使用 `elOIframe.querySelector()` 来选择后代元素

<!-- language: lang-js -->

document.querySelectorAll('.o-iframe').forEach(elOIframe => {

  const elCover = elOIframe.querySelector('.js-iframe-cover');
  const elIframe = elOIframe.querySelector(".js-iframe");
  const video = new Vimeo.Player(elIframe);

  elCover.addEventListener('click', function() {
    elCover.style.opacity = '0';
    video.play();
  });

  elCover.addEventListener('transitionend', function() {
    elCover.remove();
  });
});

<!-- language: lang-html -->

<div class="o-iframe">
  <div class="o-iframe__wrapper">
    <div class="o-iframe__cover js-iframe-cover">
      <div class="o-iframe__cover-icon"></div>
    </div>
    <iframe class="o-iframe__element js-iframe" width="560" height="315" src="" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
  </div>
</div>

<!-- 还有其他许多 <div class="o-iframe"> 元素 -->

<!-- end snippet -->

Please note that the HTML content remains unchanged in this translation.

英文:

querySelectorAll and forEach is the key here. Target every class=&quot;o-iframe&quot; elements on the page. Inside the each function target the descendants using elOIframe.querySelector()

<!-- language: lang-js -->

document.querySelectorAll(&#39;.o-iframe&#39;).forEach(elOIframe =&gt; {

  const elCover = elOIframe.querySelector(&#39;.js-iframe-cover&#39;);
  const elIframe = elOIframe.querySelector(&quot;.js-iframe&quot;);
  const video = new Vimeo.Player(elIframe);

  elCover.addEventListener(&#39;click&#39;, function() {
    elCover.style.opacity = &#39;0&#39;;
    video.play();
  });

  elCover.addEventListener(&#39;transitionend&#39;, function() {
    elCover.remove();
  });
});

<!-- language: lang-html -->

&lt;div class=&quot;o-iframe&quot;&gt;
  &lt;div class=&quot;o-iframe__wrapper&quot;&gt;
    &lt;div class=&quot;o-iframe__cover js-iframe-cover&quot;&gt;
      &lt;div class=&quot;o-iframe__cover-icon&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;iframe class=&quot;o-iframe__element js-iframe&quot; width=&quot;560&quot; height=&quot;315&quot; src=&quot;&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;!-- and many other &lt;div class=&quot;o-iframe&quot;&gt; here --&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 19:39:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定