【问题标题】:how to create HTML5 playlist loop feature如何创建 HTML5 播放列表循环功能
【发布时间】:2017-12-07 21:04:25
【问题描述】:

我正在尝试创建一个像 youtube 这样的播放列表,但我在自动播放所有视频时遇到问题,我创建了一个结束事件以在当前播放结束时播放下一个视频 这是代码

$('video').on('ended', function() {
  // Remove .active class from the active li, select next li sibling.
  var next = $('ul.nav-tabs li.active').removeClass('active').next('li');
  var tabNext = $('.tab-content .active').removeClass('active').next('div');
  var tabNextIn = $('.tab-content .in').removeClass('in').next('div');

  // Did we reach the last element? if so: select first sibling
  if (!next.length) next = next.prevObject.siblings(':first');
  if (!tabNext.length) next = next.prevObject.siblings(':first');
  if (!tabNextIn.length) next = next.prevObject.siblings(':first');

  // Add .active class to the li next in line.
  next.addClass('active');
  tabNext.addClass('active');
  tabNextIn.addClass('in');
});

if ($('.tab-pane').hasClass('active')) {
  $('.active video').attr('autoplay', '');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

我不明白为什么下一个视频不能播放

我只需要为下一个视频添加自动播放属性!

https://codepen.io/kemozzz/full/gXEzrp/

【问题讨论】:

  • 您希望它在页面加载时开始播放吗?
  • 我想自动播放下一个视频
  • "只需要为下一个视频添加自动播放属性即可!" - 这正是我的回答所暗示的。
  • 我已标记您的问题。请下次更清楚。方法如下:stackoverflow.com/help/how-to-ask
  • 谢谢,它现在完美运行@MuhammadOmerAslam

标签: javascript jquery html


【解决方案1】:

使用下面的你是不是在播放停止后触发播放见下文

$(document).ready(function() {
  setTimeout(function() {
    var vidHeight = $(".video-container").height();
    $(".description").css("height", vidHeight + "px");
  }, 500);
  $(window).resize(function() {
    var vidHeight = $(".video-container").height();
    $(".description").css("height", vidHeight + "px");
  });

  $("ul.nav-tabs a").click(function() {
    let anchor = $(this);
    setTimeout(function() {
      stopvideo();
      removeActiveLi();
      $(this).parent().addClass('active');
      var vidHeight = $(".video-container").height();
      $(".description").css("height", vidHeight + "px");
      let id = anchor.attr('href').replace('#', '');

      $('div#' + id + ' > video').addClass('active');
      playvideo();
    }, 500);
  });

  $('video').on('ended', function() {
    // Remove .active class from the active li, select next li sibling.

    if ($('.active > video').get(0).paused) {


      var next = $('ul.nav-tabs li.active').removeClass('active').next('li');
      var tabNext = $('.tab-content .active').removeClass('active').next('div');
      var tabNextIn = $('.tab-content .in').removeClass('in').next('div');

      // Did we reach the last element? if so: select first sibling
      if (!next.length) next = next.prevObject.siblings(':first');
      if (!tabNext.length) next = next.prevObject.siblings(':first');
      if (!tabNextIn.length) next = next.prevObject.siblings(':first');

      // Add .active class to the li next in line.
      next.addClass('active');
      tabNext.addClass('active');
      tabNextIn.addClass('in');
      playvideo();
    }


  });

  function playvideo() {
    if ($('.tab-pane').hasClass('active')) {
      $('.active > video').get(0).play();
    }
  }

  function stopvideo() {

    $('.active > video').removeClass('active');
    document.querySelector('.active > video').pause();
  }

  function removeActiveLi() {
    $('ul.nav-tabs li.active').removeClass('active');
  }
  playvideo();
});
.no-padding {
  padding: 0!important;
}

body {
  height: 2000px;
}

.main-container {
  border: 2px solid #000;
  overflow: hidden;
}

.description {
  background-color: #898989;
  padding: 16px 30px;
  overflow: auto;
}

.description ul {
  margin: 0;
  padding: 0;
}

.description ul li a {
  background-color: #898989;
  color: #fff;
  padding-bottom: 18px;
  margin-bottom: 18px;
  position: relative;
  border: none;
}

.nav-tabs>li>a:hover {
  border: none;
  border-bottom: 2px solid #fff;
  background-color: #fff;
  color: #555555;
}

.description ul li a>h2 {
  font-weight: 700;
  font-size: 35px;
  margin: 0;
}

.description ul li a>p {
  font-size: 18px;
  line-height: 30px;
  margin-top: 5px;
  margin-bottom: 0;
}

.video-container {
  position: relative;
  z-index: 888;
}

.video-container .tools {
  position: absolute;
  width: 100%;
  height: 40px;
  background-color: #a4a4a4;
  left: 0;
  bottom: 0;
  z-index: 999;
}

.video-container:hover .tools {
  z-index: 1;
}

#style-1::-webkit-scrollbar {
  background-color: #898989;
}

#style-1::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);
  background-color: #adadad
}

#style-1::-webkit-scrollbar {
  width: 20px;
}

@media(min-width: 992px) {
  .description {
    border-left: 2px solid #000;
  }
  .description ul li a {
    border-bottom: 2px solid #fff;
  }
}

@media(max-width: 991px) {
  .description {
    border-left: 2px solid #000;
    white-space: nowrap;
    padding: 0;
    height: auto!important;
  }
  .description ul li a {
    display: inline-block;
    width: 34%;
    padding: 14px;
    text-align: left;
    overflow: hidden;
    min-height: 252px;
    white-space: normal;
    vertical-align: text-top;
    margin-bottom: 0;
  }
  .description ul li:after {
    content: "";
    display: inline-block;
    width: 2px;
    height: 170px;
    position: absolute;
    right: 6px;
    top: 20px;
    background-color: #fff;
  }
  .description ul li:last-child:after {
    content: "";
    display: none;
  }
  .description ul li a>h2 {
    font-size: 22px;
  }
  .description ul li a>p {
    font-size: 14px;
    line-height: 23px;
  }
}

@media screen and (min-width: 220px) and (max-width: 550px) {
  .description ul li a {
    padding: 8px;
    min-height: 200px;
  }
  .description ul li a h2 {
    font-size: 14px;
  }
  .description ul li a p {
    font-size: 10px;
    line-height: 16px;
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container-fluid">
  <div class="row">
    <div class="space"></div>
    <div id="exTab2" class="col-xs-12 no-padding main-container">
      <div class="col-md-9 no-padding video-container tab-content">
        <!-- Item-1 -->
        <div id="1" class="tabs-container tab-pane active">
          <video controls="" width="100%">
                        <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
                        <source src="test.ogg" type="video/ogg"> Your browser does not support HTML5 video.</video>
        </div>
        <div id="2" class="tabs-container tab-pane">
          <!-- Item-2 -->
          <video controls="" width="100%">
                        <source src="http://vjs.zencdn.net/v/oceans.mp4" type="video/mp4">
                        <source src="test.ogg" type="video/ogg"> Your browser does not support HTML5 video.</video>
        </div>
        <div id="3" class="tabs-container tab-pane">
          <!-- Item-3 -->
          <video controls="" width="100%">
                        <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
                        <source src="test.ogg" type="video/ogg"> Your browser does not support HTML5 video.</video>
        </div>
        <div id="4" class="tabs-container tab-pane">
          <!-- Item-4 -->
          <video controls="" width="100%">
                        <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
                        <source src="test.ogg" type="video/ogg"> Your browser does not support HTML5 video.</video>
        </div>
      </div>
      <!-- Item-2 -->
      <div class="col-md-3 description" id="style-1">
        <ul class="nav nav-tabs">
          <li class="active">
            <a data-toggle="tab" href="#1">
              <h2>Chapter 1</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit, illo vel neque sunt doloremque incidunt.
              </p>
            </a>
          </li>
          <li>
            <a data-toggle="tab" href="#2">
              <h2>Chapter 2</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit, illo vel neque sunt, rerum doloremque incidunt laudantium aliquid itaque cum error nobis.</p>
            </a>
          </li>
          <li>
            <a data-toggle="tab" href="#3">
              <h2>Chapter 3</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit.</p>
            </a>
          </li>
          <li>
            <a data-toggle="tab" href="#4">
              <h2>Chapter 4</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A non velit, illo vel neque sunt, rerum doloremque incidunt laudantium aliquid itaque.</p>
            </a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</div>

【讨论】:

  • 谢谢,它运行良好,但是当我点击另一个章节时,旧视频仍在播放,我需要它自动工作,但是当我点击另一个章节时,旧视频停止播放并且活动章节视频播放
  • @KareemSultan 刚刚更新了答案,请立即确认是否是您想要的?
  • 我为活动类添加了另一个修复程序,并重用了在文件末尾创建的play()stop() 函数来播放和停止视频@KareemSultan
  • 感谢您的尝试,但问题仍然存在!
  • 你说的是哪个问题,现在点击不播放视频吗?
【解决方案2】:

这是因为你的 IF 语句没有触发它。

$('video').on('ended',function(){ 
    // Remove .active class from the active li, select next li sibling.
    var next = $('ul.nav-tabs li.active').removeClass('active').next('li');
    var tabNext = $('.tab-content .active').removeClass('active').next('div');
    var tabNextIn = $('.tab-content .in').removeClass('in').next('div');

    // Did we reach the last element? if so: select first sibling
    if (!next.length) next = next.prevObject.siblings(':first');
    if (!tabNext.length) next = next.prevObject.siblings(':first');
    if (!tabNextIn.length) next = next.prevObject.siblings(':first');

    // Add .active class to the li next in line.
    next.addClass('active');
    tabNext.addClass('active');
    tabNextIn.addClass('in');

    if ($('.tab-pane').hasClass('active')) {
        $('.active video').attr('autoplay','');
    }
  });
});

Codepen:https://codepen.io/anon/pen/EbzBBW

【讨论】:

  • 但是我需要自动播放第一个视频!
  • 我需要视频自动播放下一个!
【解决方案3】:

您可以在点击功能中添加如下内容:

$('.tabs-container.active video')[0].play();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 2016-05-01
    • 1970-01-01
    • 2014-05-28
    相关资源
    最近更新 更多