简体   繁体   English

画布尺寸的奇怪行为

[英]Strange behavior with dimensions of canvas

So, I'm confused about a current problem, which thankfully is easy to demonstrate to you guys. 所以,我对当前的问题感到困惑,幸好很容易向你们展示。

Here it is on JSFiddle: 这是JSFiddle:

Using the canvas : upper.height=$(document.body).height() - lower.height(); 使用画布upper.height=$(document.body).height() - lower.height();

Using a substitute div : upper.height($(document.body).height() - lower.height()); 使用替换divupper.height($(document.body).height() - lower.height());

As you can see, a small margin of a few pixels is placed after the canvas for some reason. 正如您所看到的,由于某种原因,在画布之后放置了几个像素的小边距。 It ends up creating a scrollbar, since it becomes 4 pixels taller than the viewport it's trying to fill. 它最终会创建一个滚动条,因为它比它试图填充的视口高4像素。 Why is this? 为什么是这样? I've set margin:0; 我设定margin:0; and padding:0; padding:0; for all elements. 对于所有元素。

Thanks so much! 非常感谢!

You need to set the canvas as a block element (a div is block level by default), it is inline by default which is why you are seeing the variation in behaviour: 您需要将canvas设置为块元素(默认情况下div是块级别),默认情况下它是内联的,这就是您看到行为变化的原因:

Demo Fiddle 演示小提琴

canvas{
    display:block
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM