谁能告诉我为什么这个CSS calc函数不起作用?当我检查Chrome上的元素时,它会显示"无效的属性值".
width: calc((100vw - 14px * 2) / (270px + 11px * 2));
Run Code Online (Sandbox Code Playgroud) 我有 4 个重叠的画布(充当图层),绝对定位并水平和垂直居中。
在此画布上,我想在画布中心的一列中覆盖四个 HTML/CSS 按钮(用于游戏菜单)。我是 CSS 和 HTML 的新手,我一直无法弄清楚如何让按钮在绝对定位的画布上居中。我怎样才能做到这一点?谢谢。
HTML:
<body>
<canvas id="canvas0" width=800 height=600></canvas>
<canvas id="canvas1" width=800 height=600></canvas>
<canvas id="canvas2" width=800 height=600></canvas>
<canvas id="canvas3" width=800 height=600></canvas>
</body>
Run Code Online (Sandbox Code Playgroud)
CSS:
#canvas0, #canvas1, #canvas2, #canvas3 {
position: absolute;
border: 2px solid;
right: 0;
left: 0;
top: 0;
bottom: 0;
margin: auto;
display: block;
}
#canvas0 {
z-index: 0;
}
#canvas1 {
z-index: 1;
}
#canvas2 {
z-index: 2;
}
#canvas3 {
z-index: 3;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
这是我想知道如何做的图。无论浏览器如何调整大小,按钮都保持在画布中央。

第二次编辑:如果我选择水平居中(而不是坚持垂直和水平居中),我可以做得很好。
这篇文章对我帮助很大:相对定位中的绝对定位
这是我的最终代码和jsfiddle …
我在pyglet中写了一个简单的'避免堕落的敌人'类型的游戏.对象在屏幕上方生成,向下移动,并在通过可见屏幕下方时被破坏.但是,我正在做一些非常错误的事情,程序运行的时间越长,对象的数量就越多.(它正在放慢速度,我发现了objgraph的问题.)当我删除这个类时,问题就消失了.
落下敌人类:
class Enemy(pyglet.sprite.Sprite):
def __init__(self, **kwargs):
super(Enemy, self).__init__(img=images.enemy_anim["front"], **kwargs)
self.out_of_bounds = False
def update(self, dt):
self._move(dt)
self._check_boundaries(dt)
self._check_kill_condition()
def _move(self, dt):
self.y -= ENEMY_SPEED * dt
def _check_boundaries(self, dt):
if self.y + self.height < 0:
self.out_of_bounds = True
def _check_kill_condition(self):
if self.out_of_bounds:
enemy_list.remove(self)
self.delete
Run Code Online (Sandbox Code Playgroud)
主要方法代码:
enemy_list = []
def add_enemy(*args, **kwargs):
randx = random.randint(0,
WINDOW_WIDTH - images.enemy_anim["front"].get_max_width())
randy = WINDOW_HEIGHT + images.enemy_anim["front"].get_max_height()
enemy_list.append(Enemy(x=randx, y=randy, batch=update_batch))
def update(dt):
for sprite in enemy_list:
sprite.update(dt)
#send the above functions to the pyglet …Run Code Online (Sandbox Code Playgroud)