【问题标题】:Onclick play Jquery trigger for multiple videosOnclick 播放多个视频的 Jquery 触发器
【发布时间】:2017-08-02 22:35:38
【问题描述】:

我正在尝试为每个视频创建一个触发器,以便使用 Jquery 在点击时播放,但它仅适用于我发现需要在 HTML 中应用标记数据组的方法。我正在通过可视化作曲家(wordpress)创建这个触发器,我只能为每个视频应用自定义类和 ID,所以我不能使用当前方法

代码笔 https://codepen.io/danielvianna/pen/jLBdYo

HTML

<div class="video_container">
<video class="video-ctrl" loop="" playsinline="" autoplay>
    <source src="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/seating_handbrake.mp4" type="video/mp4">
  </video>
</div>

<div>
    <span class="truss_wizard" data-group="play">clickme1</span>
    <video poster="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/truss_wizard_poster.jpg">
    <source src="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/truss_handbrake.mp4" type="video/mp4">
    </video>
</div>

<div>
    <span class="projection_wizard" data-group="play">clickme2</span>
    <video poster="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/projection_poster.jpg">
    <source src="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/projection_handbrake.mp4">
    </video>
</div>

<div>
    <span class="pipe_drape_wizard" data-group="play">clickme3</span>
    <video poster="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/Pipe-and-Drape_poster.jpg">
    <source src="http://sandbox.cast-soft.com/wp-content/uploads/2017/04/pipe_drape_handbrake.mp4" type="video/mp4">
    </video>
</div>

jQuery 代码

//making it more dynamic with a data attribute
//  <span class="square" data-group="play2">clickme2</span>

jQuery(function($) {
  //change the elements so they have a class and you can find multiple
    var $players1 = $('.truss_wizard');
    var $players2 = $('.projection_wizard');
    var $players3 = $('.pipe_drape_wizard');

    $players1.click(function() {
    //find all the playbutton elements
    $players1.filter('[data-group="'+ $(this).data('group') +'"]')
    //get all their parent divs
    .closest('div')
    //find their nested videos
    .find('video')
    //play each of them
    .each(function(){ this.play();
                    });
    $players2.click(function() {
    //find all the playbutton elements
    $players2.filter('[data-group="'+ $(this).data('group') +'"]')
    //get all their parent divs
    .closest('div')
    //find their nested videos
    .find('video')
    //play each of them
    .each(function(){ this.play();   
                         });
    $players3.click(function() {
    //find all the playbutton elements
    $players3.filter('[data-group="'+ $(this).data('group') +'"]')
    //get all their parent divs
    .closest('div')
    //find their nested videos
    .find('video')
    //play each of them
    .each(function(){ this.play();   
                         });
      }); 
     }); 
   }); 
});//function closes

CSS

.truss_wizard,.projection_wizard,.pipe_drape_wizard{
      height: 50px;
      width: 200px;
      display: block;
      background-color: #7ac810;
  font-size: 15;
}

span {
  font-color: yellow;
  font-size: 30px;
}

.video {
 margin-bottom: 100px;
}

注意:我正在使用包装器,因为我将它插入到 wordpress 中,并带有一个使用可视化作曲家构建的页面

【问题讨论】:

  • 您重复了“play2”ID。 ID 必须是唯一的。 id 的选择器只会找到一个元素。改为有一个类,然后它会找到多个。

标签: javascript jquery wordpress html5-video


【解决方案1】:

jQuery(function($) {
  //change the elements so they have a class and you can find multiple
  $(".play2").click(function() {
    //find all the play2 elements
    $(".play2")
      //get all their parent divs
      .closest('div')
      //find their nested videos
      .find('video')
        //play each of them
        .each(function(){ this.play(); });
  });
});

//making it more dynamic with a data attribute
//  <span class="square" data-group="play2">clickme2</span>

jQuery(function($) {
  //change the elements so they have a class and you can find multiple
  var $players = $('.square');
  
  $players.click(function() {
    //find all the play2 elements
    $players.filter('[data-group="'+ $(this).data('group') +'"]')
      //get all their parent divs
      .closest('div')
      //find their nested videos
      .find('video')
        //play each of them
        .each(function(){ this.play(); });
  });
});

【讨论】:

  • 我做了这些更改,但似乎所有视频都在同时播放,我想每次触发一个,但它是各自的按钮。注意:按钮与视频不在同一个 div 中,我正在尝试从作为选项卡一部分的跨度代码触发视频。我用你的代码制作的新 codepen:codepen.io/danielvianna/pen/mMOJbz
  • @DanielVianna 你所有的数据组在 codepen man 中都是一样的,值为“play”。
  • 我能够让它工作,但我仍然有问题。我意识到我无法添加数据组。我正在通过视觉作曲家破解这个。我只能将 ID 和 Class 附加到每个选项卡,但我不能添加数据组。有没有解决方案?注意:第一个视频应该不会被触发而是自动播放,所以我把它排除在功能之外。这是我可以访问的屏幕截图:i.imgur.com/qWIdZ0j.jpg
猜你喜欢
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多