【发布时间】:2021-02-04 05:00:56
【问题描述】:
在这个repl.it 中,SKY(由脚本生成)挡住了绿块和字符。
期望的输出:让“天空”和“地面”位于角色和绿色方块的后面。
有人可以解释为什么会发生这种情况以及如何解决它吗?我尝试了各种方法,例如更改 CSS 文件中的顺序,但没有任何区别。
var block = document.getElementById("block");
var hole = document.getElementById("hole");
//add this for the setInterval function
var character = document.getElementById("character");
hole.addEventListener('animationiteration', () => {
var random = -((Math.random() * 300) + 150);
var top = (random * 100) + 150;
hole.style.top = random + "px";
});
//interval function runs every 10 milliseconds
setInterval(function() {
var characterTop =
parseInt(window.getComputedStyle(character).getPropertyValue("top"));
character.style.top = (characterTop + 3) + "px";
}, 10);
* {
padding: 0;
margin: 0;
}
#game {
width: 400px;
height: 500px;
border: 1px solid greenyellow;
margin: auto;
overflow: hidden;
}
#character {
width: 50px;
height: 50px;
background-color: rgb(255, 0, 149);
position: absolute;
top: 250px;
border-radius: 50%;
}
#block {
width: 50px;
height: 500px;
background-color: greenyellow;
position: relative;
left: 400px;
animation: block 2s infinite linear;
}
@keyframes block {
0% {
left: 400px
}
100% {
left: -50px
}
}
#hole {
width: 50px;
height: 150px;
background-color: white;
position: relative;
left: 400px;
top: -500px;
animation: block 2s infinite linear;
}
.sky {
background-color: aqua;
width: 400px;
height: 500px;
position: relative;
}
.ground {
background-color: brown;
width: 400px;
height: 100px;
position: relative;
top: 500px;
}
<div class="sky">
<div class="ground">
<div id="game">
<div id="block"></div>
<div id="hole"></div>
<div id="character"></div>
</div>
<script src="script.js"></script>
</div>
</div>
【问题讨论】:
-
我的网络阻止了 repl.it。你能张贴一张它应该是什么样子的静止图像吗?
-
它应该是什么样子?我已经解释了,希望很清楚,期望的输出是什么。目前 SKY 或 GROUND 会覆盖(阻挡、放置)其他元素 - 例如角色和绿色块。
-
你在哪里解释过?我看没有这样的描述。上面只有三句话。
-
this 是想要的结果吗?如果是,我会发布一个答案来解释您所做的更改和错误。编辑:Idk 如果该链接按预期工作,则从未使用过 Repl。
-
那太好了。如果您可以提供详细的解释(对于初学者),将不胜感激。我也会改进我的问题。
标签: javascript html css