cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <h2>视频始终居中示例</h2>
<div class="samples">
  <div id="sample1">
    <h4>flex 版</h4>
    <div id="view1" class="view">
      <div id="v1" class="v v_flex">
        <video autoplay loop muted="true">
                    <source src="http://www.w3school.com.cn/i/movie.ogg">
                </video>
      </div>
      <div id="v2" class="v v_flex">
        <video autoplay loop muted="true">
                    <source src="http://www.w3school.com.cn/i/movie.ogg">
                </video>
      </div>
    </div>
  </div>
  <div id="sample2">
    <h4>object-fit 版</h4>
    <div id="view2" class="view">
      <video id="v3" class="v object_fit" autoplay loop muted="true">
                    <source src="http://www.w3school.com.cn/i/movie.ogg">
                </video>


      <video id="v4" class="v object_fit" autoplay loop muted="true">
                    <source src="http://www.w3school.com.cn/i/movie.ogg">
                </video>

    </div>
  </div>
</div>
<div id="ctrl"></div>
<div class="samples">
  <div id="sample3">
    <h4>absolute 版</h4>
    <div id="view3" class="view">
      <div id="v5" class="v v_abs">
        <video autoplay loop muted="true">
                    <source src="http://www.w3school.com.cn/i/movie.ogg">
                </video>
      </div>
      <div id="v6" class="v v_abs">
        <video autoplay loop muted="true">
                    <source src="http://www.w3school.com.cn/i/movie.ogg">
                </video>
      </div>
    </div>
  </div>
  <div id="sample4">
    <h4>background 版(Firefox only)</h4>
    <div id="view4" class="view">
      <div id="v7" class="v v_pst"></div>
      <div id="v8" class="v v_pst"></div>
    </div>
  </div>
</div>
<p>视频来源 www.w3school.com.cn</p>
            
          
!
            
              body,
.samples,
#view1,
.v_flex {
  display: flex;
}

body {
  justify-content: center;
  align-items: center;
  flex-direction: column;
  min-height: 100vh;
  margin: 0;
}

.samples {
  width: 80%;
  justify-content: space-around;
}

.view {
  width: 320px;
  height: 240px;
  font-size: 0;  /* 去间隙 */
  overflow: hidden;
}

.v {
  width: 50%;
  height: 100%;
  overflow: hidden;
}

.v_flex {
  justify-content: center;
  transition: width 2s linear;
}

.object_fit {
  object-fit: cover;
}

.object_fit, .v_abs, .v_pst{
  display: inline-block;
}

.v_abs {
  position: relative;
  writing-mode: vertical-lr;
}

.v_abs video {
  position: absolute;
  left: 0;
  right: 0;
  margin: auto;
}

.v_pst {
  background: -moz-element(#v3) center;
}

#ctrl {
  height: 5vh;
}

.v_flex:hover {
  width: 80%;
  flex: none;
}

h4 {
  text-align: center;
}
            
          
!
            
              function trackLocation2(e) {
  var offset = e.pageX - videoContainer2.offsetLeft,
    offsetRight = videoContainer2.offsetWidth - offset,
    position = ((e.pageX - videoContainer2.offsetLeft) / videoContainer2.offsetWidth) * 100;

  leftVideo = document.getElementById("v3");
  rightVideo = document.getElementById("v4");

  if (position <= 100) {
    rightVideo.style.width = (100 - position) + "%";
    leftVideo.style.width = position + "%";
  }
}

function trackLocation3(e) {
  var offset = e.pageX - videoContainer3.offsetLeft,
    offsetRight = videoContainer3.offsetWidth - offset,
    position = ((e.pageX - videoContainer3.offsetLeft) / videoContainer3.offsetWidth) * 100;

  leftVideo = document.getElementById("v5");
  rightVideo = document.getElementById("v6");

  if (position <= 100) {
    rightVideo.style.width = (100 - position) + "%";
    leftVideo.style.width = position + "%";
  }
}

function trackLocation4(e) {
  var offset = e.pageX - videoContainer4.offsetLeft,
    offsetRight = videoContainer4.offsetWidth - offset,
    position = ((e.pageX - videoContainer4.offsetLeft) / videoContainer4.offsetWidth) * 100;

  leftVideo = document.getElementById("v7");
  rightVideo = document.getElementById("v8");

  if (position <= 100) {
    rightVideo.style.width = (100 - position) + "%";
    leftVideo.style.width = position + "%";
  }
}
var positionDiv = document.getElementById("ctrl");
var videoContainer2 = document.getElementById("view2"),
  videoContainer3 = document.getElementById("view3"),
  videoContainer4 = document.getElementById("view4");

videoContainer2.addEventListener("mousemove", trackLocation2, false);
videoContainer3.addEventListener("mousemove", trackLocation3, false);
videoContainer4.addEventListener("mousemove", trackLocation4, false);
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console