Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <link href="https://players.brightcove.net/videojs-overlay/2/videojs-overlay.css" rel='stylesheet'>

<video-js id="myPlayerID"
  data-video-id="4607357817001"
  data-account="1752604059001"
  data-player="NqheO14Tj"
  data-embed="default"
  data-application-id
  class="video-js"
  controls
  width="640"
  height="360"></video-js>

  <script src="//players.brightcove.net/1752604059001/NqheO14Tj_default/index.min.js"></script>
  <script src="https://players.brightcove.net/videojs-overlay/2/videojs-overlay.min.js"></script>
              
            
!

CSS

              
                /* * The body style is just for the
 * background color of the codepen.
 * Do not include in your code.
 */
body {
  background-color: #111;
  color: #fff;
}
/*
 * Styles essential to the sample
 * are below
 */

/* Style the overlay */
.vjs-overlay {
  width: 80px;
  background-color: #000000;
  color: #fff;
  text-align: left;
}

              
            
!

JS

              
                // +++ Helpful method to convert seconds +++
/**
 * Utility to extract h/m/s from seconds
 * @param {number} secs - seconds to convert to hh:mm:ss
 */
function secondsToTime(secs) {
  var hours = Math.floor(secs / (60 * 60));
  if (hours < 10) {
    hours = "0" + hours.toString();
  } else {
    hours = hours.toString();
  };
  var divisor_for_minutes = secs % (60 * 60);
  var minutes = Math.floor(divisor_for_minutes / 60);
  minutes = minutes.toString();
  var divisor_for_seconds = divisor_for_minutes % 60;
  var seconds = Math.ceil(divisor_for_seconds);
  if (seconds < 10) {
    seconds = "0" + seconds.toString();
  } else {
    seconds = seconds.toString();
  };
  var obj = {
    "h": hours,
    "m": minutes,
    "s": seconds
  };
  return obj;
}

videojs.getPlayer('myPlayerID').ready(function () {
  var myPlayer = this;

  // +++ Add the overlay to the player +++
  myPlayer.overlay({
    "content": "<p id='adOverlay'>Ad: <span id='timeTarget'></span></p>",
    "start": "loadedmetadata"
  })

  // +++ Get reference to element containing overlay +++
  var adPlayerElement = document.querySelector('#adOverlay').parentElement;
  // Hide overlay at startup
  adPlayerElement.setAttribute('style', 'display: none');
  myPlayer.on('loadedmetadata', function () {
    var theInterval,
      timeLeftInAd;

    // +++ Check progress of ad every second +++
    // Function to be called every second during ad playback
    // Calculates time remaining and injects into overlay
    function everySecond() {
      var timeObject = secondsToTime(Math.floor(myPlayer.ima3.adPlayer.duration() - myPlayer.ima3.adPlayer.currentTime()));
      document.getElementById('timeTarget').innerHTML = timeObject.m + ':' + timeObject.s;
    }

    // +++ Clean up at ad end or ad skipped +++
    function videoCompleteOrSkipped() {
      // Stop the counter
      clearInterval(theInterval);
      // Hide the overlay
      adPlayerElement.setAttribute('style', 'display: none');
      // Clear any numbers so on display of overlay no small numbers left
      document.getElementById('timeTarget').innerHTML = '';
    }

    // +++ Start counter on start of ad +++
    myPlayer.on('ima3-started', function () {
      adPlayerElement.setAttribute('class', 'vjs-overlay');
      // Display the overlay
      adPlayerElement.setAttribute("style", "display: block; left: 5px");
      // Start the counter that calls function every second
      theInterval = setInterval(everySecond, 1000);
    });

    // +++ Listen for ad end or skipped +++
    myPlayer.on('ima3-complete', videoCompleteOrSkipped);
    myPlayer.on('ads-ad-skipped', videoCompleteOrSkipped);
  });
});

              
            
!
999px

Console