Hovering over a thumbnail triggers audio but not video using the YouTube API in JavaScript – how can I fix this?

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

Hovering over a thumbnail triggers audio but not video using the YouTube API in JavaScript - how can I fix this?

问题

我正在尝试在悬停在缩略图上时播放YouTube视频,但它只播放音频,我看不到视频,而且当我停止悬停时它不会停止播放。

这是代码

HTML

<div class="thumbnail" data-video-id="D9N7QaIOkG8">
    <img src="assets\meyra.jpg" alt="Thumbnail 1">
    <div class="video-overlay"></div>
</div>

<script>
  function onYouTubeIframeAPIReady() {
    const thumbnails = document.querySelectorAll(".thumbnail");

    thumbnails.forEach(function(thumbnail) {
      const videoOverlay = thumbnail.querySelector(".video-overlay");
      const videoId = thumbnail.dataset.videoId;

      thumbnail.addEventListener("mouseenter", function() {
        const player = new YT.Player(videoOverlay, {
          videoId: videoId,
          width: "100%",
          height: "100%",
          playerVars: {
            autoplay: 1,
            controls: 0,
            rel: 0,
            showinfo: 0
          }
        });

        videoOverlay.style.display = "block";
      });

      thumbnail.addEventListener("mouseleave", function() {
        videoOverlay.innerHTML = "";
        videoOverlay.style.display = "none";
      });
    });
  }
</script>

我希望视频在悬停时覆盖缩略图,就像YouTube中的预览一样,并且当鼠标停止悬停时停止播放。

英文:

I am trying to make a YouTube video play when I hover on a thumbnail
But it only plays the audio, I can't see the video and it doesn't stop playing when I stop hovering.

Here is the code

HTML

<div class="thumbnail" data-video-id="D9N7QaIOkG8">
    <img src="assets\meyra.jpg" alt="Thumbnail 1">
    <div class="video-overlay"></div>
</div>

<script>
  function onYouTubeIframeAPIReady() {
    const thumbnails = document.querySelectorAll(".thumbnail");

    thumbnails.forEach(function(thumbnail) {
      const videoOverlay = thumbnail.querySelector(".video-overlay");
      const videoId = thumbnail.dataset.videoId;

      thumbnail.addEventListener("mouseenter", function() {
        const player = new YT.Player(videoOverlay, {
          videoId: videoId,
          width: "100%",
          height: "100%",
          playerVars: {
            autoplay: 1,
            controls: 0,
            rel: 0,
            showinfo: 0
          }
        });

        videoOverlay.style.display = "block";
      });

      thumbnail.addEventListener("mouseleave", function() {
        videoOverlay.innerHTML = "";
        videoOverlay.style.display = "none";
      });
    });
  }
</script>

I want a video to cover up the thumbnail just like a preview in youtube when I hover and stop playing when the mouse stops hovering.

答案1

得分: 0

问题在于一旦你悬停鼠标,YouTube会用iframe替换你的**.videoOverlay** div。所以你在videoOverlay变量中选择的引用将不再适用于显示和隐藏。

你需要将

videoOverlay.style.display = "block";

替换为

this.querySelector('.video-overlay').style.display = "block";

在mouseleave事件处理程序内执行相同的操作

this.querySelector('.video-overlay').innerHTML = "";
this.querySelector('.video-overlay').style.display = "none";

编辑:
要与视频对象交互(比如播放或停止视频),你需要获取播放器的引用,像下面这样。

function onYouTubeIframeAPIReady() {
  const thumbnails = document.querySelectorAll(".thumbnail");

  var player;
  thumbnails.forEach(function(thumbnail) {
    const videoOverlay = thumbnail.querySelector(".video-overlay");
    const videoId = thumbnail.dataset.videoId;

    thumbnail.addEventListener("mouseenter", function() {
      player = new YT.Player(videoOverlay, {
        videoId: videoId,
        width: "100%",
        height: "100%",
        playerVars: {
          autoplay: 1,
          controls: 0,
          rel: 0,
          showinfo: 0
        }
      });

      this.querySelector('.video-overlay').style.display = "block";
    });

    thumbnail.addEventListener("mouseleave", function() {
      this.querySelector('.video-overlay').innerHTML = "";
      this.querySelector('.video-overlay').style.display = "none";
      player.destroy();
    });
  });
}
英文:

The problem is once you mouseover, youtube replace your .videoOverlay div with iframe. So the reference you selected in videoOverlay variable will no longer work for showing and hiding.

You need to replace

videoOverlay.style.display = "block";

With

this.querySelector('.video-overlay').style.display = "block";

Do the same thing inside mouseleave event handler

this.querySelector('.video-overlay').innerHTML = "";
this.querySelector('.video-overlay').style.display = "none";

EDIT:
To interact with video object, (like you need to play or stop video), then you need to get a reference of player like below.

function onYouTubeIframeAPIReady() {
  const thumbnails = document.querySelectorAll(".thumbnail");

  var player;
  thumbnails.forEach(function(thumbnail) {
    const videoOverlay = thumbnail.querySelector(".video-overlay");
    const videoId = thumbnail.dataset.videoId;

    thumbnail.addEventListener("mouseenter", function() {
      player = new YT.Player(videoOverlay, {
        videoId: videoId,
        width: "100%",
        height: "100%",
        playerVars: {
          autoplay: 1,
          controls: 0,
          rel: 0,
          showinfo: 0
        }
      });

      this.querySelector('.video-overlay').style.display = "block";
    });

    thumbnail.addEventListener("mouseleave", function() {
      this.querySelector('.video-overlay').innerHTML = "";
      this.querySelector('.video-overlay').style.display = "none";
      player.destroy();
    });
  });
}

huangapple
  • 本文由 发表于 2023年5月25日 21:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332920.html