123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <p>Only want to insert HTML5 video if certain conditions are met? Say you're using a video as an animated background, but iOS isn't going to play that video inline. Or you may need to only control a video in Javascript. Why waste the users bandwidth loading a video that can't be used?</p>

<p>View the source to see that there are no inline <code>&lt;video&gt;</code> or <code>&lt;img&gt;</code> elements. They are all added via Javascript so you have greater control over when & where.</p>

<div class="insert-video" data-controls="controls" data-mute="mute" data-preload="auto" data-sources='[{"src":"http://techslides.com/demos/sample-videos/small.webm","type":"video/webm"},{"src":"http://techslides.com/demos/sample-videos/small.ogv","type":"video/ogg"},{"src":"http://techslides.com/demos/sample-videos/small.mp4","type":"video/mp4"},{"src":"http://techslides.com/demos/sample-videos/small.3gp","type":"video/3gp"}]' data-srcset='[{"src":"https://www.quirksmode.org/html5/videos/big_buck_bunny.mp4","type":"video/mp4"},{"src":"https://www.quirksmode.org/html5/videos/big_buck_bunny.webm","type":"video/webm"},{"src":"https://www.quirksmode.org/html5/videos/big_buck_bunny.ogv","type":"video/ogg"}] 1x, [{"src":"http://techslides.com/demos/sample-videos/small.webm","type":"video/webm"},{"src":"http://techslides.com/demos/sample-videos/small.ogv","type":"video/ogg"},{"src":"http://techslides.com/demos/sample-videos/small.mp4","type":"video/mp4"},{"src":"http://techslides.com/demos/sample-videos/small.3gp","type":"video/3gp"}] 2x'>
  <noscript>Fallback for no Javascript. You could link to the video file, or embed the video anyway.</noscript> 
</div>

<hr />

<h1>Video to Placeholder Converter</h1>

<p>Paste your <code>&lt;video&gt;</code> or sources here for a quick conversion to the <code>insert-video</code> placeholder format.</p>

<textarea id="source-convert">
<video controls="controls" mute="mute" preload="auto">
  <source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm" /> 
  <source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg" /> 
  <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4" />
  <source src="http://techslides.com/demos/sample-videos/small.3gp" type="video/3gp" />
</video>
</textarea>
<pre id="source-convert-output"></pre>

<hr />

<h1>Fallbacks</h1>

<p>This <code>insertVideos</code> condition is set to <code>false</code> so the <code>data-fallback</code> attribute will be inserted instead.</p>

<div class="fail-video" data-controls="controls" data-mute="mute" data-preload="auto" data-fallback='<img src="http://lorempixel.com/400/225/animals/7/" alt="" />'>
</div>

<p>This insert video is going to fail so the <code>data-poster</code> attribute will be inserted as an image since there isn't a <code>data-fallback</code> attribute or any fallback set in the <code>insertVideos()</code> call.</p>

<div class="fail-video" data-controls="controls" data-mute="mute" data-preload="auto" data-poster='http://lorempixel.com/400/225/nature/3/'>
  test.
  <!--  -->
</div>
            
          
!
            
              body { padding: 1em; max-width: 35em; margin: auto; font-size: 15px; font-family: 'Helvetica', sans-serif; line-height: 1.4; }
code { font-size: 0.9em; background: #eee; padding: 2px 4px; }
video, img { display: block; width: 100%; height: auto; }
hr { display: block; margin: 2em auto; border: none; border-top: solid 1px #aaa; }

textarea { 
  display: block;
  width: 100%;
  height: 150px;
  font-family: monospace;
  font-size: 12px;
}
#source-convert-output {
  background: #eee;
  white-space: pre-wrap;
  font-size: 12px;
  padding: 4px;
}
            
          
!
            
              (function(){

  var d = document,
      strType = typeof '',
      defaultSelector = '.insert-video';

  function each(obj,callback) {
    var length = obj.length,
        i = 0;

    for ( ; i < length; i++ ) {
      if ( callback.call( obj[ i ], i, obj[ i ] ) === false ) { break; }
    }
  }

  function createVideo(vidAttributes){
    var video = d.createElement('video'),
        sources = vidAttributes.sources;

    if ( !sources || vidAttributes.src ) { return false; }
    delete vidAttributes.sources;

    for (var attr in vidAttributes) { video.setAttribute(attr,vidAttributes[attr]); }

    sources = JSON.parse(sources);

    each(sources,function(){
      var source = d.createElement('source');
      for (var attr in this) { source.setAttribute(attr,this[attr]); }
      video.appendChild(source);
    });

    return video;
  }


  function insertVideos(selector,opts){

    if ( arguments.length === 1 ) {
      opts = selector || {};
      selector = defaultSelector;
    } else {
      opts = opts || {};
      selector = selector || defaultSelector;
    }

    var elems = (
          selector.nodeType ? [selector] :
          typeof selector === strType ? d.querySelectorAll(selector) : selector
        ),
        videos = [];

    if ( !elems ) { return false; }

    each(elems,function(i,elem){

      if ( elem && elem.className.indexOf(' insert-video--done') < 0 ) {

        var dataAttr = elem.dataset || false,
            attr, video;

        if ( !dataAttr ) {
          attr = elem.attributes;

          each(attr,function(i,el){
            var attrName = this.name.replace('data-','');
            if ( attrName.length < this.name.length ) {
              dataAttr[attrName] = this.value;
            }
          });

        }

        if ( opts.condition !== false ) {
          video = createVideo(dataAttr);
        }

        if ( video ) {
          videos.push(video);
          elem.innerHTML = '';
          elem.className += ' insert-video--done';
          elem.appendChild(video);
        } else {
          elem.className += ' insert-video--fallback';
          elem.innerHTML = ( dataAttr.fallback ? dataAttr.fallback : opts.fallback ? opts.fallback : dataAttr.poster ? '<img src="' + dataAttr.poster + '" />' : '' );
        }


      }
    });

    return videos;
  }

  window.insertVideos = insertVideos;

}());

insertVideos({
  condition: Modernizr.video
});

insertVideos('.fail-video',{
  condition: false
})
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console