[英]CSS: z-index doesn't work prperly when i append the new div element
I want to implement the website about draw some rectangles and stickers configured by div element.我想实现关于绘制一些由 div 元素配置的矩形和贴纸的网站。
To add sticker or rectangle, User click the button.要添加贴纸或矩形,用户单击按钮。
I want to arrange div element in the order of below我想按以下顺序排列 div 元素
(upper) sticker > rack > rectangle > canvas (lower) (上)贴纸 > 架子 > 矩形 > canvas(下)
So I used z-index in style sheet.所以我在样式表中使用了 z-index。
#canvas {
position: relative;
z-index: 1;
...
}
.rectangle {
position: absolute !important;
z-index: 2 !important;
...
}
.rack {
position: absolute !important;
z-index: 3 !important;
...
}
.sticker {
position: absolute !important;
z-index: 4 !important;
...
}
It works well, when elements are allocated by html code.当元素由 html 代码分配时,它运行良好。
<div id="canvas">
<div class="rectangle draggable resizable"
style="left:0px; top:0px; width:200px; height:300px;"
type="rectangle" id="rectangle_org">
<div class="rack draggable resizable"
style ="left: 0; top:40px; width: 50px; height: 100px;"></div>
<div class="sticker sticker_green draggable"
type="sticker" id="green"
style="left:0px; top:0px;">
</div>
<div class="sticker sticker_yellow draggable"
type="sticker" id="yellow"
style="left:30px; top:0px;"></div>
<div class="sticker sticker_red draggable"
type="sticker" id="red"
style="left:60px; top:0px;">
</div>
<div class="sticker sticker_gray draggable"
type="sticker" id="gray"
style="left:90px; top:0px;">
</div>
</div>
</div>
When I clicked the button to append element to canvas, it doen't work properly.当我点击 append 元素到 canvas 的按钮时,它无法正常工作。
the new rectangle cover the stickers, even though the z-index of the rectangle is lower than sticker's.新的矩形覆盖了贴纸,即使矩形的 z-index 低于贴纸的。
I think the z-index of these are like below我认为这些的 z-index 如下所示
(upper) new sticker > new rack > new rectangle > old sticker > old rack > old rectangle (lower) (上)新贴纸>新货架>新矩形>旧贴纸>旧货架>旧矩形(下)
How can i fix this error.我该如何解决这个错误。
Please refer this fiddle ( http://jsfiddle.net/crisply/bD35Z/9/ )请参考这个小提琴( http://jsfiddle.net/crisply/bD35Z/9/ )
为了使用z-index属性, 您必须将div在CSS中的位置定义为: 绝对,相对或固定
When you append a new element to the DOM the z-index
goes back to 0
. 当您将新元素附加到DOM时,
z-index
返回0
。 I haven't tested this on other browsers, but it happens in Chrome. 我尚未在其他浏览器上进行过测试,但是它发生在Chrome中。
You have to either set z-index
manually on the appended element, or code some logic to automatically assign z-index
values to your elements. 您必须在附加元素上手动设置
z-index
,或者编写一些逻辑以自动将z-index
值分配给元素。
What @Pier mentioned seems to be right. @Pier 提到的似乎是正确的。 Adding
添加
z-index: -1;
to the block you already created in JS seems to work and stickers no longer are covered by it.到您已经在 JS 中创建的块似乎可以工作并且贴纸不再被它覆盖。 But if you want to create more than 1 block, then this trick stops working (because accordingly to the answer above- z-index in the whole website goes back to 0 or sth like that)
但是如果你想创建超过 1 个块,那么这个技巧就会停止工作(因为根据上面的答案-整个网站中的 z-index 会回到 0 或类似的东西)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.