如何创建HTML5播放列表循环功能

时间:2017-12-07 10:57:07

标签: javascript jquery html5

我正在尝试像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>

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

我只需要为下一个视频添加autoplay属性!

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

3 个答案:

答案 0 :(得分:1)

使用以下内容,播放停止后不会触发播放,请参阅下文

&#13;
&#13;
$(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();
});
&#13;
.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;
  }
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是因为您的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

答案 2 :(得分:0)

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

{
"_id" : ObjectId("5a290637e4b08ddd7334e541"),
"placeName" : "Caspeco Mobile Order",
"created" : ISODate("2017-12-07T09:13:27.248Z"),
"currency" : "SEK",
"isCredit" : false,
"costCenter" : {
    "name" : "Test Restaurangen",
    "number" : "800"
},
"debitAccounts" : [ 
    {
        "name" : "CASH",
        "number" : "1915",
        "amount" : 189.0
    }
],
"creditAccounts" : [ 
    {
        "name" : "Mat",
        "number" : "3120",
        "amount" : 168.749984741211
    }
],
"taxAccounts" : [ 
    {
        "name" : "Moms 12%",
        "number" : "2621",
        "amount" : 20.2500076293945
    }
],
"credit" : false