简体   繁体   English

使视频比较滑块全宽/高

[英]Make video comparison slider full width/height

I'm looking to create a full height/width viewport video comparison slider inspired by this article: 我正在寻找一个受本文启发的全高/宽视口视频比较滑块:

Article 文章

I'm trying to make the video take the full height and width of the viewport, but I've been unsuccessful so far, and I'm unsure how to achieve the effect. 我正在尝试使视频占据视口的整个高度和宽度,但是到目前为止,我一直没有成功,而且我不确定如何实现这种效果。 Any help would be much appreciated, link to JSFiddle example and code below: 任何帮助将不胜感激,请链接到JSFiddle示例和下面的代码:

JS Fiddle JS小提琴

HTML: HTML:

<div id="video-compare-container">
  <video loop autoplay poster="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/dirty.jpg">
    <source src=https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/floodplain-dirty.mp4>
    <source src=https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/floodplain-dirty.webm>
  </video>
 <div id="video-clipper">
    <video loop autoplay poster="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/clean.jpg">
      <source src=https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/floodplain-clean.mp4>
      <source src=https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/floodplain-clean.webm>
    </video>
  </div>
    </div>

CSS 的CSS

body {
  background: #333;
  margin:0;
}
#video-compare-container {
  display: inline-block;
  line-height: 0;
  position: relative;
  width: 100%;
  padding-top: 42.3%;
}
#video-compare-container > video {
  width: 100%;
  position: absolute;
  top: 0; height: 100%;
}
#video-clipper {
  width: 50%; position: absolute;
  top: 0; bottom: 0;
  overflow: hidden;
}
#video-clipper video {
  width: 200%;
  postion: absolute;
  height: 100%;
}

Javascript: Javascript:

function trackLocation(e) {
  var rect = videoContainer.getBoundingClientRect(),
      position = ((e.pageX - rect.left) / videoContainer.offsetWidth)*100;
  if (position <= 100) { 
    videoClipper.style.width = position+"%";
    clippedVideo.style.width = ((100/position)*100)+"%";
    clippedVideo.style.zIndex = 3;
    }
}
var videoContainer = document.getElementById("video-compare-container"),
videoClipper = document.getElementById("video-clipper"),
clippedVideo = videoClipper.getElementsByTagName("video")[0];
videoContainer.addEventListener( "mousemove", trackLocation, false); 
videoContainer.addEventListener("touchstart",trackLocation,false);
videoContainer.addEventListener("touchmove",trackLocation,false);

I'd use canplaythrough events on both videos, to check if they are ready: 我会在两个视频上都使用canplaythrough事件,以检查它们是否准备就绪:

video1.addEventListener("canplaythrough", function() {
    // video1 ready to be played
}, false);

This event is fired when browser estimates that i has enough data to continuesly play streaming. 当浏览器估计我有足够的数据来继续播放流时,将引发此事件。

Only then i'd play both the videos simultaneousely: 只有这样我才能同时播放两个视频:

video1.play()
video2.play()

PS. PS。 I assumed that you've got problem with synchronising the footage 我认为您在同步镜头方面遇到问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM