视频全屏弹幕实现

1次阅读

共计 2772 个字符,预计需要花费 7 分钟才能阅读完成。

该文章短小精悍问?视频全屏后如何加弹幕答:了解一下 screenfull; pointer-events: none;(如果你没有领会我的意思,我可以手摸手带你撸一遍)

视频全屏遇到的问题 最近在搞给视频加个花边的东西用的 Flash, 在全屏的时候浏览器会把视频层级提高(H5 也有同样的问题),(重点是 Flash 并不草理你的 DOM,大家玩玩就懂我的意思了。)导致全屏后通过 position 定位的花边位于了视频下方,简单的说花边在视频全屏的时候不展示

处理问题的过程 最开始用的是非常常规的想法,如何在视频全屏的时候降低视频的层级,使我需要的 div 块位于视频上方。然后尝试了各种方法,结果被制裁的服服帖帖的,大家可以去搞搞。其实仔细思考我要做的就是类似于弹幕的东西,咦~~B 站怎么实现的。

别人家的弹幕 B 站肯定是拿不到源码的啦,缕他控制台的资源,element,style。人家确实全屏的时候实现了,弹幕 写的 DOM 结构,和 CSS 样式并不复杂,全屏无外乎全屏的时候给 DOM 加了一个样式而已。我也照着试了不行。

WEY?
4. 错在了哪里
这个问题如果思路错了就是个死胡同。所以我在找问题的时候趟了很多坑。思路错在我不应该使用视频的全屏,敲黑板!划重点啦!不要使用视频的全屏!(解决了问题的时候才幡然醒悟),然后发现了个贼有意思的东西。[screenfull][3]。不知道 B 站他们怎么玩的,但是我用这个实现了。这个东西做了一个事情让你浏览器的 DOM 铺满全屏。欧?~~~
5 实现它 DOM
<div class=’player’ id=’vb’>
<video id=’vd’ playsinline webkit-playsinline=”true” controls style=’object-fit: contain;’>
<source src=”http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4″ type=”video/mp4″ />
</video>
<div class=’video-status’ id=’log’>
<img src=”http://wx4.sinaimg.cn/large/006ARE9vgy1fs9w9ukwjhj30g40afaap.jpg” alt=””>
</div>
</div>
Script
<script src=”https://cdn.bootcss.com/screenfull.js/4.1.0/screenfull.js”></script>
<script>
const log = document.getElementById(‘log’)
const videoBox = document.getElementById(‘vb’)
log.onclick = _=> {
const fullFlag = videoBox.className.indexOf(‘full-screen’)
if (fullFlag === -1) {
videoBox.setAttribute(‘class’, ‘player full-screen’)
} else {
videoBox.setAttribute(‘class’, ‘player’)
}
screenfull.toggle()
}
</script>
CSS
<style>
/* reset */
html,body,h1,h2,h3,h4,h5,h6,div,dl,dt,dd,ul,ol,li,p,blockquote,pre,hr,figure,table,caption,th,td,form,fieldset,legend,input,button,textarea,menu{margin:0;padding:0;}
header,footer,section,article,aside,nav,hgroup,address,figure,figcaption,menu,details{display:block;}
table{border-collapse:collapse;border-spacing:0;}
caption,th{text-align:left;font-weight:normal;}
html,body,fieldset,img,iframe,abbr{border:0;}
i,cite,em,var,address,dfn{font-style:normal;}
[hidefocus],summary{outline:0;}
li{list-style:none;}
h1,h2,h3,h4,h5,h6,small{font-size:100%;}
sup,sub{font-size:83%;}
pre,code,kbd,samp{font-family:inherit;}
q:before,q:after{content:none;}
textarea{overflow:auto;resize:none;}
label,summary{cursor:default;}
a,button{cursor:pointer;}
h1,h2,h3,h4,h5,h6,em,strong,b{font-weight:bold;}
del,ins,u,s,a,a:hover{text-decoration:none;}
body,textarea,input,button,select,keygen,legend{font:12px/1.14 arial,\5b8b\4f53;color:#333;outline:0;}
body{background:#fff;}
a,a:hover{color:#333;}

.player {
width: 800px;
height: auto;
margin: 0 auto;
position: relative;
background-color: black;
}
.player video{
width: 100%;
height: 100%;
}
.video-status {
position: absolute;
right: 100px;
top: 40px;
}
.video-status img {
width: 100px;
}
.full-screen {
position: fixed;
width: 100%;
height: 100%;
}
</style>
去我的代码里找你需要的东西吧。

6.pointer-events 是啥?弹幕呢?
pointer-events 是一个 CSS 属性做点透的时候用的,如果你有一个弹幕层在 Video 上边你会需要到她的。弹幕呢,没有。好多人都已经实现了好不好,这个你还是去问问 [百度][5] 或者 [必应][6] 吧。

GitHub 地址 https://github.com/Gao-Fan/vi…

正文完
 0