javascript属性在更改后不会更改

zdwk9cvp  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(342)

我有一个动态显示流媒体视频(webrtc会议)的网页,为此,我还必须根据视频数量动态设置视频元素大小。
我意识到我的javascript代码只改变视频元素的大小一次(当我第一次应用它时)。或者可能是我做错了什么。因为它可以很好地工作,如果我硬编码的视频数量(只有html和js;没有webrtc)和js必须一次性设置大小。。我的webrtc代码在开始时总是一个视频,当它循环通过(当新视频出现时),那么我的代码就无法处理所有元素的大小。
我的代码:

// I have to reuse this code everytime I have to change size (properties) of all video tags in the document

function layout() {
    for(var i=0;i<videoIds.length;i++){ // I've tried with ids
        var localView = document.getElementById(videoIds[i]);
        localView.setAttribute('position', 'relative');
        localView.setAttribute('height', '50vh');
        localView.setAttribute('width', '50vw');
        localView.setAttribute('object-fit', 'cover');
    }

    var localView = document.getElementsByClassName('class'); // I've tried with class name
    for(var i=0;i<localView.length;i++){
        localView.setAttribute('position', 'relative');
        localView.setAttribute('height', '50vh');
        localView.setAttribute('width', '50vw');
        localView.setAttribute('object-fit', 'cover');
    }

    var localView = document.getElementsByTagName('video'); // I've tried with tag name
    for(var i=0;i<localView.length;i++){
        localView.setAttribute('position', 'relative');
        localView.setAttribute('height', '50vh');
        localView.setAttribute('width', '50vw');
        localView.setAttribute('object-fit', 'cover');
    }
}

我有什么遗漏吗?请帮忙。
是否有可能在再次调用代码时我的代码没有完成,或者跳过了一些代码。例子:

const initSelfStream = (id) => {
    (async () => {
        try {
            await navigator.mediaDevices.getUserMedia(
                {
                    //video: { facingMode: selectedCamera },
                    video: video_constraints,
                    audio: true
                }).then(stream => {
                    const video = document.createElement("video");
                    loadAndShowVideoView(video, stream, localId);
                });
        } catch (err) {
            console.log('(async () =>: ' + err);
        }
    })();
}
initSelfStream(id);

// Some Code

pc.ontrack(e){
    const video = document.createElement("video");
    loadAndShowVideoView(video, e.tracks[0], remoteId);
}

const loadAndShowVideoView = (video, stream, id) => {
    if(totalUsers.length == 1){
        console.log('Currently ' + totalUsers.length + ' User Are Connected.');
        video.classList.add('video-inset', 'background-black');
        video.setAttribute("id", id);
        video.style.position = "absolute"

        video.srcObject = stream;
        video.style.height = 100+"vh"
        video.style.width = 100+"vw"

        video.muted = true;
        video.style.objectFit = "cover"

        appendVideo(video, stream);
    } else if(totalUsers.length == 2){
        // I add new video with desirable height & width
        // Then call and run original function on the top with element number & condition
         layout('8 videos', "size should be 25vh, 25vw");
    }
}

const appendVideo = (video, stream) => {
    container.append(video);
    video.play()
}

html非常简单。正文中包含视频的一个div:

<body>
    <div class="views-container background-black" id="container"></div>
</body>
w41d8nur

w41d8nur1#

尝试组合所有样式属性。而不是:

video.style.position = "absolute"
video.style.height = 100+"vh"
video.style.width = 100+"vw"
video.style.objectFit = "cover"

你可以:

video.setAttribute('style', 'position: absolute; height: 100vh; width: 100vw; object-fit: cover;);

由于您已经使用了css类,您不必在您的条件下重复它,只需:

video { position: absolute; top: 0; left: 0; obejct-fit: cover; }

现在你可以改变了 widthheight 具有 setAttribute .

相关问题