样式化 HTML5 视频控件

Posted

技术标签:

【中文标题】样式化 HTML5 视频控件【英文标题】:Styling HTML5 Video Controls 【发布时间】:2013-12-22 07:50:14 【问题描述】:

我似乎无法在视频播放器控件上正确设置样式,因为似乎没有正确对齐。

我已尝试将项目包装在 div 和 lis 中,但似乎无法正确移动或对齐。

有什么建议吗?

到目前为止我所拥有的: http://jsfiddle.net/pp6Wn/

var vid, playbtn, seekslider, curtimetext, durtimetext, mutebtn, volumeslider, fullscreenbtn;

function intializePlayer() 
  // Set object references
  vid = document.getElementById("my_video");
  playbtn = document.getElementById("playpausebtn");
  seekslider = document.getElementById("seekslider");
  curtimetext = document.getElementById("curtimetext");
  durtimetext = document.getElementById("durtimetext");
  mutebtn = document.getElementById("mutebtn");
  volumeslider = document.getElementById("volumeslider");
  fullscreenbtn = document.getElementById("fullscreenbtn");
  // Add event listeners
  playbtn.addEventListener("click", playPause, false);
  seekslider.addEventListener("change", vidSeek, false);
  vid.addEventListener("timeupdate", seektimeupdate, false);
  mutebtn.addEventListener("click", vidmute, false);
  volumeslider.addEventListener("change", setvolume, false);
  fullscreenbtn.addEventListener("click", toggleFullScreen, false);

window.onload = intializePlayer;

function playPause() 
  if (vid.paused) 
    vid.play();
    playbtn.style.background = "url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxNS4xLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+DQo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3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WxkIDApICAtLT4NCjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+DQo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4Ig0KCSB3aWR0aD0iMThweCIgaGVpZ2h0PSIxOHB4IiB2aWV3Qm94PSI5MSA5MSAxOCAxOCIgZW5hYmxlLWJhY2tncm91bmQ9Im5ldyA5MSA5MSAxOCAxOCIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+DQo8Zz4NCgk8cGF0aCBvcGFjaXR5PSIwLjk1IiBmaWxsPSIjRkZGRkZGIiBlbmFibGUtYmFja2dyb3VuZD0ibmV3ICAgICIgZD0iTTkzLjI1OCw5MS4yMzlsMTQuNjkyLDcuNjgxYzEuMTQxLDAuNTk2LDEuMTQxLDEuNTYzLDAsMi4xNTkNCgkJbC0xNC42OTIsNy42ODNjLTEuMTQsMC41OTctMi4wNjQsMC4wMzctMi4wNjQtMS4yNDhWOTIuNDg3QzkxLjE5NCw5MS4yMDIsOTIuMTE4LDkwLjY0Myw5My4yNTgsOTEuMjM5eiIvPg0KPC9nPg0KPC9zdmc+DQo=)";
    playbtn.style.backgroundSize = "100% 100%";
  


function vidSeek() 
  var seekto = vid.duration * (seekslider.value / 100);
  vid.currentTime = seekto;


function seektimeupdate() 
  var nt = vid.currentTime * (100 / vid.duration);
  seekslider.value = nt;
  var curmins = Math.floor(vid.currentTime / 60);
  var cursecs = Math.floor(vid.currentTime - curmins * 60);
  var durmins = Math.floor(vid.duration / 60);
  var dursecs = Math.floor(vid.duration - durmins * 60);
  if (cursecs < 10) 
    cursecs = "0" + cursecs;
  
  if (dursecs < 10) 
    dursecs = "0" + dursecs;
  
  if (curmins < 10) 
    curmins = "0" + curmins;
  
  if (durmins < 10) 
    durmins = "0" + durmins;
  
  curtimetext.innerhtml = curmins + ":" + cursecs;
  durtimetext.innerHTML = durmins + ":" + dursecs;


function vidmute() 
  if (vid.muted) 
    vid.muted = false;
    mutebtn.innerHTML = "Mute";
   else 
    vid.muted = true;
    mutebtn.innerHTML = "Unmute";
  


function setvolume() 
  vid.volume = volumeslider.value / 100;


function toggleFullScreen() 
  if (vid.requestFullScreen) 
    vid.requestFullScreen();
   else if (vid.webkitRequestFullScreen) 
    vid.webkitRequestFullScreen();
   else if (vid.mozRequestFullScreen) 
    vid.mozRequestFullScreen();
  
#video-container,
#playpausebtn,
#fullscreenbtn,
#mutebtn,
#curtimetext,
#durtimetext,
#seekslider,
#volumeslider 
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;

#video-container 
  position: relative;

div#video_controls_bar 
  position: absolute;
  background: #61676D;
  bottom: 0;
  left: 0;
  right: 0;
  margin: 0 auto 5% auto;
  width: 60%;
  height: 35px;
  padding-top: 10px;
  padding-bottom: 10px;
  padding-right: 5%;
  padding-left: 5%;
  -webkit-border-radius: 12px;
  -moz-border-radius: 12px;
  border-radius: 12px;
  opacity: 0;
  -webkit-transition: opacity .3s;
  -moz-transition: opacity .3s;
  -o-transition: opacity .3s;
  -ms-transition: opacity .3s;
  transition: opacity .3s;

#video-container:hover #video_controls_bar 
  opacity: 1;

button#playpausebtn 
  background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxNS4xLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+DQo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4Ig0KCSB3aWR0aD0iMThweCIgaGVpZ2h0PSIxOHB4IiB2aWV3Qm94PSI5MSA5MSAxOCAxOCIgZW5hYmxlLWJhY2tncm91bmQ9Im5ldyA5MSA5MSAxOCAxOCIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+DQo8Zz4NCgk8cGF0aCBvcGFjaXR5PSIwLjk1IiBmaWxsPSIjRkZGRkZGIiBlbmFibGUtYmFja2dyb3VuZD0ibmV3ICAgICIgZD0iTTkzLjI1OCw5MS4yMzlsMTQuNjkyLDcuNjgxYzEuMTQxLDAuNTk2LDEuMTQxLDEuNTYzLDAsMi4xNTkNCgkJbC0xNC42OTIsNy42ODNjLTEuMTQsMC41OTctMi4wNjQsMC4wMzctMi4wNjQtMS4yNDhWOTIuNDg3QzkxLjE5NCw5MS4yMDIsOTIuMTE4LDkwLjY0Myw5My4yNTgsOTEuMjM5eiIvPg0KPC9nPg0KPC9zdmc+DQo=);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  width: 7%;
  height: 30px;
  cursor: pointer;
  border: 0;
  background-color: red;

button#playpausebtn:hover 
  cursor: pointer;

button#fullscreenbtn 
  background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxNS4xLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ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OTksMC43aDQuOTc2YzAuMzg1LDAsMC43LTAuMzE0LDAuNy0wLjdWMS4xOTljMC0wLjM4NS0wLjMxNC0wLjY5OS0wLjctMC42OTlINS40NDkNCgkJQzUuMDY0LDAuNSw0Ljc1LDAuODE0LDQuNzUsMS4xOTlWMi41OWMwLDAuMzg1LTAuMjIyLDAuNDc4LTAuNDk1LDAuMjA1bC0xLjgwMS0xLjhDMi4xODMsMC43MjIsMS43MzYsMC43MjEsMS40NjIsMC45OTENCgkJbC0wLjUwNywwLjVjLTAuMjczLDAuMjctMC4yNzQsMC43MTMtMC4wMDEsMC45ODNsMS43OTgsMS43ODRDMy4wMjQsNC41MjgsMi45MzQsNC43NSwyLjU0OSw0Ljc1aC0xLjM1DQoJCUMwLjgxNCw0Ljc1LDAuNSw1LjA2NCwwLjUsNS40NDlWNi4xNzV6Ii8+DQo8L2c+DQo8L3N2Zz4NCg==);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  width: 7%;
  height: 40px;
  cursor: pointer;
  border: 0;
  background-color: blue;

button#mutebtn 
  width: 10%;
  background-color: green;

#curtimetext,
#durtimetext 
  width: 5%;
  display: inline-block;
  vertical-align: middle;

input#seekslider 
  width: 35%;
  height: 20px;
  background-color: yellow;

input#volumeslider 
  width: 10%;
  background-color: red;

input[type='range'] 
  -webkit-appearance: none !important;
  background: #384047;
  -webkit-border-radius: 12px;
  -moz-border-radius: 12px;
  border-radius: 12px;

input[type='range']::-webkit-slider-thumb 
  -webkit-appearance: none !important;
  background: #6CBB7C;
  height: 20px;
  width: 20px;
  border-radius: 100%;
  cursor: pointer;

input[type="range"]::-webkit-slider-thumb:hover 
  -webkit-animation: pulse 1s infinite;

@-webkit-keyframes pulse 
  from 
    -webkit-box-shadow: 0 0 2px transparent;
  
  50% 
    -webkit-box-shadow: 0 0 10px #e74c3c;
  
  to 
    -webkit-box-shadow: 0 0 2px transparent;
  

@media screen and (max-width: 500px) 
  #curtimetext,
  #durtimetext 
    display: none;
  
<div id="video-container">
  <video id="my_video"  >
    <source src="http://romain-menard.fr/cours_html5/sitedemo/src/small.mp4" type="video/mp4" />
    <source src="video/movie.webm" type="video/webm" />
  </video>
  <!-- Video Controls -->
  <div id="video_controls_bar">
    <button id="playpausebtn"></button>

    <span id="curtimetext">00:00</span>
    <input id="seekslider" type="range" min="0" max="100" value="0" step="1"><span id="durtimetext">00:00</span>
    <button id="mutebtn">Mute</button>
    <input id="volumeslider" type="range" min="0" max="100" value="100" step="1">
    <button id="fullscreenbtn"></button>
  </div>
</div>

这就是我想要的效果

【问题讨论】:

效果很好。它只需要一些样式更改 【参考方案1】:

我已经更新了你的 jsfiddle 来为你解决这个问题,你的元素需要添加以下 CSS:

.class
    display: inline-block;
    vertical-align: middle;

http://jsfiddle.net/pp6Wn/2/

【讨论】:

【参考方案2】:

你可以使用弹性盒子:

#video_controls_bar
  display: flex;
  align-items: center; // All items vertically centered.

【讨论】:

以上是关于样式化 HTML5 视频控件的主要内容,如果未能解决你的问题,请参考以下文章

在移动端怎么将HTML5中的video标签的控件去掉或者隐藏

有没有办法为 HTML5 的范围控件设置样式?

CSS 样式表HTML5响应式网页设计视频教程

样式化 HTML5 输入类型编号

在 Chrome 中样式化 HTML5 数字输入(旋转框)

IE 没有样式化 HTML5 标签(使用 shiv)