andy jones andy jones - 1 year ago 219
Javascript Question

Html5 video not playing on bxslider

Currently i have a bxSlider, which has a video on the second slide. I've got the video working inside of the slider fine with the video tag. But what I'm trying to do is get the video to play once the slide is active and current.

This is my current on slide after.

onSlideAfter: function(currentSlide, totalSlides, currentSlideHtmlObject){


if(currentSlideHtmlObject == 1){
var video = document.getElementById("video");
} else if(currentSlideHtmlObject == 2 || currentSlideHtmlObject == 0) {
$j('video')[0].currentTime = 0;


Ignore my console log, I was just seeing if the .play did anything. It doesn't. Yet the video doesn't seem to play once the slide shows. If i set the video to auto play it will work, but this just doesn't seem to work.

If I run:


I get the following: (which i don't fully understand).

Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}


Answer Source

You simply use the callback onSlideBefore(). More details in the source in the comments.


<!doctype html>

  <meta charset="utf-8">
  <title>Multi BxSlider</title>
  <link href="" rel="stylesheet" />
    /* This centers the img and video */
    video {
      display: block;
      margin: 0 auto;


  <ul class="bx">
    <li data-idx="0">
      <img src="">
    <li data-idx="1">
      <!-- Set each video id so that it corresponds with the `data-idx`
      So if slide <li> is data-idx="1", then video id="vid1"-->
      <video id="vid1" class="vid" src="" controls></video>
    <li data-idx="2">
      <img src="">
    <li data-idx="3">
      <img src="">
    <li data-idx="4">
      <img src="">

  <script src=""></script>
  <script src=""></script>
    var bx = $(".bx").bxSlider({
      pager: false,
      nextText: '',
      prevText: '',
      infiniteLoop: false,
      This callback will fire a function 
      before a slide completes it's
      course to it's destination
      onSlideBefore: autoPlay
    This function just uses the `to` parameter
    I put all of them there because bxSlider expects 
    them there, I have bad luck if I don't... don't ask:P
    function autoPlay($ele, from, to) {
      idx is the slide index number
      if there is a video within 
      the slide then play it.
      var idx = parseInt(to, 10);  
      var vid = document.querySelector('#vid'+idx);
      if (vid) {;


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download