8

我有一个 div,我想用相同的宽度、高度、填充和边距将画布完全覆盖在它上面。虽然我使用 position: absolute 就像这里推荐的大多数问题一样,z-index 画布仍然显示在 div 下方。这是我到目前为止的代码。

<div id ="editor-section">  
     <div class="editable" id="editor"></div>
</div> 

// the canvas is created / removed dynamically on connection / disconection 
hub.client.broadcastSomeoneConnected = function (connectionId, someoneConnected) 
{
    if (someoneConnected) {
        var canvas = document.createElement("canvas");
        canvas.id = connectionId;
        canvas.className = 'canvases';
        canvas.style.border = '2px solid red';
        canvas.style.zIndex = zindex;  
        zindex++;

        var parentDiv = document.getElementById("editor-section");
        parentDiv.appendChild(canvas);

    } else { // someone disconnected

        var canvas = document.getElementById(connectionId);
        canvas.parentNode.removeChild(canvas);
    }
 }

 // css for all canvases
 .canvases {
     width:60%; 
     height:700px;
     border:1px solid;
     position:absolute;
     padding: 5%;
     margin-left:20px;
     pointer-events: none;
 }

 // css for editor div
 #editor {
     padding: 5%;
     margin-left:20px;
     border: 2px solid black;
     overflow-y:scroll;
     height: "700px";
     width: "100%";
     background-color: white;
     margin-bottom:30px;
 }

PS:zindex 是全局初始化的,因为我需要多个相互叠加的画布,编辑器最终取决于连接的人数

我在这里做错了什么?谢谢

4

1 回答 1

12

差不多好了。在容器上移动宽度和高度,使其相对,编辑器和画布绝对定位在 0,0 处,宽度/高度为 100%。编辑器的zIndex必须低于zIndex画布的。小提琴

于 2013-05-11T22:43:26.703 回答