最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - bootstrap carousel - pause the html video while sliding - Stack Overflow

programmeradmin1浏览0评论

I have bootstrap carousel which includes both images and videos and it works fine. But when we move to next slide, currently playing video in active slide should be paused.

Now the video is still playing even after moving to next slide.

Any help is much appreciated.

Thanks!!

$('#myCarousel').carousel({
  interval: 3000
});

DEMO

I have bootstrap carousel which includes both images and videos and it works fine. But when we move to next slide, currently playing video in active slide should be paused.

Now the video is still playing even after moving to next slide.

Any help is much appreciated.

Thanks!!

$('#myCarousel').carousel({
  interval: 3000
});

DEMO

Share asked Feb 12, 2016 at 9:22 user3932810user3932810 3
  • I am using html5 video tag not iframe – user3932810 Commented Feb 12, 2016 at 10:07
  • @Praveen Kumar it is not duplicate as per above ment – Morpheus Commented Feb 12, 2016 at 10:30
  • @user3932810 Opened it. – Praveen Kumar Purushothaman Commented Feb 12, 2016 at 10:33
Add a ment  | 

3 Answers 3

Reset to default 4

You can call a pause event on html5 video:

document.getElementById('someelement').pause()

More video events here

Answering your question - you can use slide.bs.carousel event bined with the above line to stop video when slide event occurs:

$('#myCarousel').carousel({
  interval: 3000
}).on('slide.bs.carousel', function () {
  document.getElementById('player').pause();
});

See the updated jsfiddle

the best way is to start the fist one using autoplay and the you can use jquery to start and stop them. I have several carousel items where only the first 3 have videos.

I solved it like this:

<script language="JavaScript" type="text/javascript">
        $(document).ready(function () {
            $('.carousel').carousel({ interval: 8000 })
            $('#myCarousel').on('slide.bs.carousel', function (args) {
                var videoList = document.getElementsByTagName("video");
                switch (args.from) {
                    case 0:
                        videoList[0].pause();
                        break;
                    case 1:
                        videoList[1].pause();
                        break;
                    case 2:
                        videoList[2].pause();
                        break;
                }
                switch (args.to) {
                    case 0:
                        videoList[0].play();

                        break;
                    case 1:
                        videoList[1].play();
                        break;
                    case 2:
                        videoList[2].play();
                        break;
                }
            })

        });
    </script>

this assumes that the videos in your DOM are ordered in the order of your carousel and that there are no videos above, as you have a case you could just grab the video by it's ID, that would always work.

If you are using Youtube iframe videos, then I achieved this by listening for the carousel slide event slide.bs.carousel :

  • https://getbootstrap./docs/4.4/ponents/carousel/#events

Then if this event occurs, I would then use player.pauseVideo() functionality of the Youtube iframe API with JavaScript:

  • https://developers.google./youtube/iframe_api_reference#Playback_controls

Sample snippet:

// When a slide occurs, pause the current iframe video that is playing
// player.pauseVideo():Void - Pauses the currently playing video.
// Reference: https://developers.google./youtube/iframe_api_reference#Playback_controls
$('#moviesCarousel').on('slide.bs.carousel', function(event) {
    // The variable "players" contain each Youtube Player for each iframe video
    // Reference: https://developers.google./youtube/iframe_api_reference#Loading_a_Video_Player
    // event.from - The index of the current video (before the slide occurs)
    //            - It is also the index of the corresponding player for the current video
    // Reference: https://getbootstrap./docs/4.4/ponents/carousel/#events
    players[event.from].pauseVideo();
});

Where:

  • event.from corresponds to the index of the carousel video item before the slide occurred
  • players are a list of YT.Player instances, where each instance controls 1 particular iframe Youtube video (so 1 video item among the carousel video list). This assumes that the order of carousel videos maps to the same order of its corresponding YT.Player instances

For a plete working html code, please refer to my answer in another thread:

  • Pause Bootstrap carousel when playing Youtube video
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>