【发布时间】:2020-08-21 13:59:35
【问题描述】:
我正在使用 Nodejs、socket.io 和 WebRTC(peerjs 库)开发视频聊天应用程序。在登录页面上,用户插入他/她的名字并被重定向到他的视频流(通过 WebRTC)并可以与他的同行连接的页面。这工作正常,视频正在 DOM 中动态添加。一旦每个同伴加入,我想使用 Javascript 在他/她的视频的右下角动态附加每个登录用户的名称。
在下面的函数中,我添加了一个静态名称,仅用于调试目的
JavaScrpt 函数
//Function that appends all the videos to the DOM (Working fine)
const videoGrid = document.getElementById('video-grid');
const addVideoStream = (video, stream) => {
video.srcObject = stream
video.addEventListener('loadedmetadata', () => {
video.play()
})
videoGrid.append(video)
//Here I create a div, add the name in it and append on top of video
const testName = "John Doe"
const div = document.createElement('div');
div.classList.add('video-name');
const html = `
<i class="fas fa-microphone"></i>
<span>${testName}</span>
`
div.innerHTML = html;
video.appendChild(div);
}
标记
<div class="main__videos">
<div id="video-grid">
{/* Add videos dynamically via Js */}
</div>
</div>
CSS
//CSS
//Parent Container that holds all videos
#video-grid{
display: flex;
width: 1090px;
overflow-y: auto;
flex-wrap: wrap;
position: relative;
}
//Each video
video{
height: 250px;
width: 350px;
object-fit: cover;
padding: 8px;
position: relative;
}
//Name styles that are being added dynamically
.video-name {
justify-content: end;
position:absolute;
left: 0;
z-index:1;
color: red;
background-color: orange;
}
【问题讨论】:
标签: javascript css webrtc peerjs