javascript - 在 html5 中文本以视频为中心

标签 javascript jquery css html html5-video

我的容器 div 包含一个视频和一个包含一些文本的 div

我希望文本位于视频上方并位于中心位置,当我调整浏览器窗口大小时也是如此。

我意识到这个demo ,但我认为这只是一个起点。我该如何改进我的代码?

这是代码。

/**CSS**/

video {
  position:relative;
  z-index:-1;
}

#custom-message {
  position:relative;
  color: rgb(230, 200, 98);
  z-index:1;
  top: -200px;       
}
<!--**HTML**-->

<div id="container" align="center">
  <video width="640" height="320" autoplay>
    <source src="http://media.w3.org/2010/05/sintel/trailer.mp4" type="video/mp4"/>
  </video>
  <div id="custom-message">CENTERED TEXT ABOVE</div>
</div>

最佳答案

请看这里的片段: DEMO

var $vid = $('video','#container');
var $msg = $('#custom-message'); 
$msg.css({
    top:$vid.offset().top + (($vid.height()/2) - ($msg.height()/2)),
    left:$vid.offset().left + (($vid.width()/2) - ($msg.width()/2))
});​

关于javascript - 在 html5 中文本以视频为中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13876937/

相关文章:

jquery - CSS 将多个没有模式的第 n 个子元素放入一个选择器中

jquery - 仅当内容溢出父div时,如何才能使内容水平滑动?

javascript - 在 angularJS 中使用 html ID

javascript - react browserHistory.push 给出未捕获的类型错误 : Cannot read property 'push' of undefined

javascript - 如何将项目插入到特定索引处的空数组中?

javascript - 自动提交表单信息,无需重新加载页面

javascript - 在每个函数中的最后一个元素动画之后执行一些操作

javascript - 关闭当前标签

javascript - 当我将鼠标悬停在另一个 div 上时,使用效果更改标题图像

html - 从右边缘开始的 CSS 背景图 block