为了账号安全,请及时绑定邮箱和手机立即绑定

Bootstrap Carousel - 在活动幻灯片上自动播放视频

Bootstrap Carousel - 在活动幻灯片上自动播放视频

Helenr 2023-07-14 10:15:04
我一直在尝试使用 Bootstrap Carousel 制作一部互动纪录片。为此,我希望活动幻灯片上的视频自动播放。我读过,除非视频静音,否则不可能自动播放,但如果您只需单击一次音量按钮即可自动播放所有内容,我不介意,但我还不知道如何做到这一点。在 Chrome 上,不会自动播放任何内容,您必须手动播放每个视频。使用 Safari 时,一旦用户之前与页面进行过交互,视频就会自动播放,但问题是它们都会同时自动播放,即使视频位于不活动的幻灯片上(有声音)。有谁知道解决方案吗?我的HTML:<div id="myCarousel" class="carousel slide" data-interval="false"><ol class="carousel-indicators">    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>    <li data-target="#myCarousel" data-slide-to="1"></li>    <li data-target="#myCarousel" data-slide-to="2"></li>  </ol>  <div class="carousel-inner">    <div class="carousel-item active">         <video controls autoplay loop muted class="myvid" id="player">            <source src="assets/img/intro.mp4" type="video/mp4">            </video>    </div>    <div class="carousel-item">         <video controls autoplay class="myvid" id="player2">            <source src="assets/img/Placeholder Video.mp4" type="video/mp4">            </video> </div>  </div>  <a class="carousel-control-prev" href="#myCarousel" role="button" data-slide="prev">    <span class="carousel-control-prev-icon" aria-hidden="true"></span>    <span class="sr-only">Vorige</span>  </a>  <a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">    <span class="carousel-control-next-icon" aria-hidden="true"></span>    <span class="sr-only">Volgende</span>  </a></div>我的 JavaScript:  }).on('slide.bs.carousel', function () {  document.getElementById('player').pause();});/* SLIDE ON CLICK */ $('.carousel-indicators > li > a').click(function() {    // grab href, remove pound sign, convert to number    var item = Number($(this).attr('href').substring(1));    // slide to number -1 (account for zero indexing)    $('#myCarousel').carousel(item - 1);    // remove current active class    $('.carousel-indicators .active').removeClass('active');    // add active class to just clicked on item    $(this).parent().addClass('active');    // don't follow the link    return false;});提前致谢。
查看完整描述

1 回答

?
千万里不及你

TA贡献1784条经验 获得超9个赞

您可以这样使用Bootstrap 4 Carousel 事件:


let allVids = $("#myCarousel").find('.carousel-item');


allVids.each(function(index, el) {

  if (index !== 0) {

    $(this).find('video')[0].pause();

  }

});


$("#myCarousel").on('slide.bs.carousel', function(ev) {

  let slides = $(this).find('.carousel-item');

  let pvid = slides[ev.from].querySelectorAll('video')[0];

  let vid = slides[ev.to].querySelectorAll('video')[0];

  let isPlaying = vid.currentTime > 0 && vid.readyState > 2;


  vid.play();


  if (isPlaying) {

    pvid.pause();

  }

});

#myCarousel {

  max-width: 1200px;

  margin: 0 auto;

}


.carousel-control-next, .carousel-control-prev {

    width: 9% !important;

}


.carousel-inner {

  background: #111;

}


.carousel-indicators {

  bottom: -13px !important;

}


.carousel-item {

  padding-bottom: 25px;

}


video {

  width: 100%;

}

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.0/js/bootstrap.min.js" integrity="sha384-3qaqj0lc6sV/qpzrc1N5DC6i1VRn/HyX4qdPaiEFbn54VjQBEU341pvjz7Dv3n6P" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.0/css/bootstrap.min.css" integrity="sha384-SI27wrMjH3ZZ89r4o+fGIJtnzkAnFs3E4qz9DIYioCQ5l9Rd/7UAa8DHcaL8jkWt" crossorigin="anonymous">



<div id="myCarousel" class="carousel slide" data-interval="false">

  <ol class="carousel-indicators">

    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>

    <li data-target="#myCarousel" data-slide-to="1"></li>

    <li data-target="#myCarousel" data-slide-to="2"></li>

  </ol>


  <div class="carousel-inner">

    <div class="carousel-item active">

      <video controls autoplay loop muted class="myvid" id="player">

            <source src="//commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4">

            </video>

    </div>


    <div class="carousel-item">

      <video controls autoplay class="myvid" id="player2">

            <source src="//commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4" type="video/mp4">

            </video>

    </div>


    <div class="carousel-item">

      <video controls autoplay class="myvid" id="player2">

            <source src="//commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4" type="video/mp4">

            </video>

    </div>

  </div>


  <a class="carousel-control-prev" href="#myCarousel" role="button" data-slide="prev">

    <span class="carousel-control-prev-icon" aria-hidden="true"></span>

    <span class="sr-only">Vorige</span>

  </a>

  <a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">

    <span class="carousel-control-next-icon" aria-hidden="true"></span>

    <span class="sr-only">Volgende</span>

  </a>

</div>


查看完整回答
反对 回复 2023-07-14
  • 1 回答
  • 0 关注
  • 181 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信