Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

jquery - Start autoplaying video by clicking image overlay. Need to work for multiple videos

I have a bit of code working which when clicking on an overlay images removes the image and allow it's corresponding video to autoplay. What I am struggling with is how to make this work if there are multiple videos, each with its own overlay image.

Here is a link to the codepen: https://codepen.io/NewbCake/pen/qMMQZo

The problem is that when clicking on any overlay it removes all overlays and plays only the first video. How can I make it work for each one individually? I notice when I change the index of f = $f[0] to f = $f[1] it plays the second video (even clicking on the first video). How do I get it to select the index of the one that it is clicked on?

I am also wondering if there are any foreseeable drawbacks to using this...will work on mobile or will it cause problems?

Thank you in advance for any help!

HTML

<section>
  <div class="video-intro">
    <div class="image">
      <div class="play-button btn"></div>
      <a href="javascript:" id="btn_play" class="btn">
        <img src="http://placekitten.com/960/540" alt="play video" />    
      </a>
    </div>
    <iframe src="https://player.vimeo.com/video/66991893?api=1&title=0&amp;byline=0&amp;portrait=0&amp;color=57c0d4" width="960" height="540" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
  </div>
</section>

CSS

section {
  display:grid;
  grid-template-columns:10vw 500px;
}

/* video intro */
.video-intro {
  grid-column: 2 ;
  box-shadow:0px 0px 7px #ddd;
  margin:0 auto 50px !important;
  width:90%;
  position:relative;
  max-width:960px;

.image {
  position:absolute;
  top:0;
  left:0;
  z-index:20;

img {
  width:100%;
  height:auto;
}

JS

$(function(){
  var $parent = $('.video-intro'),
  $f = $parent.find('iframe'),
  $image = $parent.find('.image'),
  f = $f[0],
  url = $f.attr('src').split('?')[0];

  window.addEventListener('message', function(e){
    var d = JSON.parse(e.data);
  }, false);

  $('.btn').click(function(){
    var $t = $(this);
    runCommand('play');
    $image.hide();
  });

  function runCommand(cmd){
    var data = {method : cmd};
    f.contentWindow.postMessage(JSON.stringify(data), url);
  }

  // fitvids
  $('.video-intro').fitVids();

});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this:

This will play the corresponding video, and if you click on another one it will pause the current one and play the new one. Hope this helps :)

$(function(){

      window.addEventListener('message', function(e){
        var d = JSON.parse(e.data);
      }, false);


      $('.btn').click(function(){

        parent_video = $(this).parent().parent();

        $('.video-intro').each(function(){
             if(!$(parent_video).is($(this))){
               var $iframe = $(this).find('iframe');
               $iframe[0].src = $iframe[0].src;
               $(this).find('.image').show(0);
             }
        });

        var $t = $(this);
        var $iframe = $t.parent().parent().find('iframe');
        setTimeout(function(){
          runCommand('play', $iframe);
        }, 200);

        $t.parent().fadeOut(200); //hide the .image div
      });


      function runCommand(cmd, $f){
        var data = {method : cmd},
        url = $f.attr('src').split('?')[0];
        $f[0].contentWindow.postMessage(JSON.stringify(data), url);
      }

      // fitvids
      $('.video-intro').fitVids();

});

https://codepen.io/andrejagovski/pen/XPyajE


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...