简体   繁体   English

为什么出现垂直滚动条时html5 canvas不全屏

[英]why html5 canvas is not full screen when vertical scrollbar appears

I'm using a full screen canvas as background of the first section of my page.我使用全屏 canvas 作为页面第一部分的背景。 But as soon as I add the second section and vertical scrollbar appears, the height of canvas reduces a little bit and a gap appears.但是当我添加第二部分并出现垂直滚动条时,canvas 的高度会降低一点,并出现间隙。 here's my code:这是我的代码:
PS: Sorry, my code contained bugs, I fixed them. PS:对不起,我的代码包含错误,我已修复它们。 now you can see the red gap.现在您可以看到红色间隙。

 var canvas = document.getElementById('canvas') var c = canvas.getContext('2d') scaleCanvas() window.addEventListener("resize", scaleCanvas) function scaleCanvas() { canvas.width = window.innerWidth canvas.height = window.innerHeight c.fillStyle = 'black' c.fillRect(0, 0, canvas.width, canvas.height) }
 * { box-sizing: border-box; max-width: 100%; } body { margin: 0; padding: 0; } #first-section { position: relative; min-height: 100vh; background-color: red; /* to see the gap */ } #content { position: relative; z-index: 2; } #second-section { min-height: 100vh; background-color: blue; } #canvas { display: block; padding: 0; margin: 0; border: none; position: absolute; top: 0; left: 0; z-index: 1; }
 <div id="first-section"> <canvas id="canvas"></canvas> <div id="content">content</div> </div> <div id="second-section"></div>

Assuming you mean full screen, and not full page.假设您的意思是全屏,而不是整页。 The two are very different.两者非常不同。

If you mean full page then the link to the Screen API will also give you details on obtaining the page size.如果您的意思是整页,那么屏幕API 的链接也会为您提供有关获取页面大小的详细信息。

Size full screen canvas.尺寸全屏canvas。

The problem is that you have content that extends outside the page width and height ( innerWidth , innerHeight )问题是您的内容超出了页面宽度和高度( innerWidthinnerHeight

The elements with ids first-section , content , and second-section must be inside the display area or else you will get a scroll bar. id first-sectioncontentsecond-section的元素必须在显示区域内,否则您将获得滚动条。 The scroll bar will change the innerWidth , innerHeight values subtracting the scrollbar width or height depending on which is visible.滚动条将更改innerWidthinnerHeight值减去滚动条宽度或高度,具体取决于哪个可见。

To prevent scroll bars the best option is to keep all content inside innerWidth , and innerHeight为了防止滚动条,最好的选择是将所有内容保留在innerWidthinnerHeight

Full screen with scroll bars.全屏滚动条。

If you want have the scroll bars and you are using full screen you can use the Screen API to get the width and height of the display in pixels.如果您想要滚动条并且您正在使用全屏,您可以使用屏幕API 来获取显示的widthheight (以像素为单位)。 You can set the canvas size to match the screen without the scroll bars effecting its size.您可以设置 canvas 大小以匹配屏幕,而滚动条不会影响其大小。

Note Do read the provided link to Screen as what defines the screen may not be as expected.注意 请阅读提供的屏幕链接,因为定义屏幕的内容可能与预期不同。 EG more than one monitor, or device orientation will effect how you use the API. EG 多台显示器或设备方向会影响您使用 API 的方式。

Basic example基本示例

Thus when in full-screen mode you can set the canvas size and ignore scroll bars with因此,在全屏模式下,您可以设置 canvas 大小并忽略滚动条

function sizeFullScreenCanvas() {
  canvas.width = screen.width;
  canvas.height= screen.height;
}

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

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