【发布时间】:2019-06-09 21:22:40
【问题描述】:
我正在使用 HTML5 和 CSS 网格创建基于精灵的游戏。 我为不同的元素(背景和前景)设置了不同的 z-index
期望: 我想象多个元素可以占据同一个网格空间,但显然,它们被移动了,忽略了 z-index。
结果:更改前景元素的网格区域会改变剩余项目的顺序,而不是重叠它们
CSS:
#container {
position: relative;
height: 500px;
width: 500px;
display: grid;
grid-gap: 1px;
grid-template-columns: repeat(5, 20%);
grid-template-rows: repeat(5, 20%);
}
.item {
z-index: 0;
display: flex;
justify-content: center;
align-items: center;
background-size: 1600%;
image-rendering: pixelated;
}
@keyframes walk {
100% { background-position: -462px; }
}
#sprite {
z-index: 2;
background-color: transparent;
height: 100px;
width: 100px;
background: url(./../assets/thief.png) 0px 0px;
background-size: 1200%;
image-rendering: pixelated;
animation: walk 0.5s steps(5) infinite;
}
【问题讨论】:
-
正在通过编辑样式生成元素在网格中的位置:
{ gridColumn: ${this.state.hero.c}, gridRow: ${this.state.hero.r}, } -
你是剩下的代码还是小提琴?为什么精灵的位置有时会设置在网格之外?
标签: html css z-index css-grid layer