Search code examples
javascriptjqueryhtmlcssplyr.js

Play multiple Plyr Vimeo embeds on hover


I have multiple videos embedded on a page with Plyr.js. My end goal is to get each video to play on hover and stop when there is no hover.

Here is my current code:

const players = Array.from(document.querySelectorAll('#player')).map(p => new Plyr(p, {
  debug: true,
  volume: 0,
  controls: false,
  muted: true,
  fullscreen: { enabled: false },
  loop: { active: true },
  duration: 10
}));

$('#player').hover(playVideo, pauseVideo);
function playVideo(e){$(players, this).get(0).play();}
function pauseVideo(e){$(players, this).get(0).pause();}
.plyr{
  display:inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/plyr/3.4.7/plyr.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="player" class="plyr__video-embed">
  <iframe src="https://player.vimeo.com/video/298038460?loop=1&byline=0&portrait=0&title=0&transparent=0" allowtransparency></iframe>
</div>
<div id="player" class="plyr__video-embed">
  <iframe src="https://player.vimeo.com/video/298038460?loop=1&byline=0&portrait=0&title=0&transparent=0" allowtransparency></iframe>
</div>

The code above plays only the first video on hover but does not play other videos.

If anyone has any suggestions please feel free to share. Any help will be highly appreciated.


Solution

  • First issue is the duplicate id values. They need to be unique. You already have a class (.plyr__video-embed) for those elements so use that instead.

    Your hover methods also try to find the player inside the hovered element, but that will not work because players array holds instances of the player plugin and not DOM nodes.

    So just find the index of the hovered element (amongst the player nodes) and use it to access the relevant player.

    const playerNodes = $('.plyr__video-embed');
    const players = playerNodes.map((i,p) => new Plyr(p, {
      debug: true,
      volume: 0,
      controls: false,
      muted: true,
      fullscreen: { enabled: false },
      loop: { active: true }
    })).get();
    
    playerNodes.hover(playVideo, pauseVideo);
    
    function playVideo(e){players[playerNodes.index(this)].play();}
    function pauseVideo(e){players[playerNodes.index(this)].pause();}
    .plyr {
      display: inline-block;
    }
    
    .plyr__video-embed {
      border: 1px solid red;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/plyr/3.4.7/plyr.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="plyr__video-embed">
      <iframe src="https://player.vimeo.com/video/298038460?loop=1&byline=0&portrait=0&title=0&transparent=0" allowtransparency></iframe>
    </div>
    <div class="plyr__video-embed">
      <iframe src="https://player.vimeo.com/video/298038460?loop=1&byline=0&portrait=0&title=0&transparent=0" allowtransparency></iframe>
    </div>