javascript - 从 JS 代码设置为 0 时,CSS flex 属性没有正确响应
问题描述
这可能是我对某些事情的一个非常简单的疏忽,但到目前为止,我有一个非常简单的项目,它将屏幕分为三个部分,前两个部分在一个容器中,display: flex
并且它们都被赋予相同的 flex 值1. 在我的 JS 代码中,当一个特定的布尔变量设置为 false 时,我希望这两个部分中的一个被隐藏,而另一个占据它的位置。如果我在 CSS 中设置 'flex: 0',这将按预期工作,但在 JS 中设置时则不然。
// Init on page load
window.addEventListener("load", function() {
var gameContainer = document.getElementById("game-container");
if (!false) { //this is to be changed later on to check for a boolean value
gameContainer.style.flex = "0"; //this should hide the right part, but it does not
}
});
#whole-game {
display: flex;
}
#story-container {
background-color: black;
height: 80vh;
flex: 1;
}
#game-container {
background-color: blue;
height: 80vh;
flex: 1;
}
#settings-container {
background-color: rgb(83, 50, 8);
width: 100vw;
height: 20vh;
}
<body>
<div id="whole-game">
<div id="story-container"></div>
<div id="game-container"></div>
</div>
<div id="settings-container"></div>
</body>
解决方案
正如我在您的代码中看到的,您已将“gameManager”变量声明为数组而不是对象。所以,如果要执行“试用”函数,首先需要访问数组中的对象索引,然后函数:
var gameManager = [{
},
{trial: function (){
var gameContainer = document.getElementById("game-container");
if(!false){ //this is to be changed later on to check for a boolean value
gameContainer.style.flex = "0"; //this should hide the right part, but it does not
}},
}]
// Init on page load
window.addEventListener("load", gameManager[1].trial);
当然,您要将 index [1] 更改为对象的实际索引。
推荐阅读
- tfs - TFS-2018 中不提供手动干预
- android - 通知渠道的重要性会被单个通知的重要性覆盖吗?
- java - 将通用 RecyclerView 适配器转换为 Kotlin
- c# - 尝试设置始终覆盖表单但具有静态/固定纵横比的背景图像
- unity3d - 在使用 Unity 的 VRTK 的任何场景中都看不到控制器
- github - GitHub 未加载“net::ERR_CONNECTION_CLOSED”
- c++ - 如何读取/询问文件系统和文件结构
- c++ - 通过使用标量参数的运算符覆盖修改类成员
- sql-server - SQL Server - 计算两次之间的小时数并将这些小时分成三个时间段
- sungridengine - 来自主服务器的 Sun Grid Engine (SGE) 电子邮件通知