简体   繁体   English

使用 window.innerWidth 和 window.innerHeight 使滚动条出现

[英]Using window.innerWidth and window.innerHeight makes scrollbars appear

I'm trying to make a simple canvas that stretches itself to fully fill the viewport, but I don't want scrollbars to appear, a thing that happens if I try to resize the canvas in JS using window.innerWidth and window.innerHeight .我正在尝试制作一个简单的画布,它可以自行拉伸以完全填充视口,但我不希望出现滚动条,如果我尝试使用window.innerWidthwindow.innerHeight在 JS 中调整画布的大小,就会发生这种情况。

Note: I don't want to resize the canvas in CSS because the elements inside get all stretched out since from what I've understood the CSS treats the canvas as an img, and doesn't actually stretch the resolution of the canvas.注意:我不想在 CSS 中调整画布的大小,因为里面的元素都被拉伸了,因为据我所知,CSS 将画布视为 img,并且实际上并没有拉伸画布的分辨率。

This is what happens:这是发生了什么: 在此处输入图像描述 This is the code: css:这是代码:CSS:

    html,
body {
    height: 100%;
    width: 100%;
    margin:0;
    padding:0;
}

/*canvas*/
#bg {
    background: #171629;
}

js: js:

ctx.beginPath()
ctx.canvas.width  =  window.innerWidth
ctx.canvas.height = window.innerHeight


ctx.rect(20, 40, 50, 50)
ctx.fillStyle = "#e5e5e5"
ctx.fill()
ctx.closePath()

After a bunch of trying I found this as a solution:经过一系列尝试,我发现这是一个解决方案:

css: CSS:

html,
body {
  display:flex;
  margin:0;
  padding:0;
}

js: js:

canvasElement.height = window.innerHeight;
canvasElement.width = document.body.clientWidth;

You can also use:您还可以使用:

css: CSS:

body {
   margin: 0;
   overflow: hidden;
}

In fact, My problem was when I typed事实上,我的问题是当我输入

<style type="text/csss">
  body{
      margin:0;
      overflow:hidden;
  }
  canvas{
      background-color:lightblue;
  }</style>

instead of:代替:

<style type="text/css">
  body{
      margin:0;
      overflow:hidden;
  }
  canvas{
      background-color:lightblue;
  } </style>

a tiny grammatical mistake can make you suffer a lot一个微小的语法错误会让你遭受很多痛苦

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

相关问题 不正确的 window.innerWidth 和 window.innerHeight - incorrect window.innerWidth and window.innerHeight window.innerHeight和window.innerWidth的缓存值 - Cache values of window.innerHeight and window.innerWidth 将 backgroundSize 设置为 window.innerWidth 和 window.innerHeight - Setting backgroundSize to window.innerWidth and window.innerHeight Android浏览器的screen.width,screen.height&window.innerWidth&window.innerHeight不可靠 - Android browser's screen.width, screen.height & window.innerWidth & window.innerHeight are unreliable 将 div 居中放置在可响应 window.innerWidth 和 window.innerHeight 大小的画布之上 - Centering a div on top of a canvas that is responsively sized to window.innerWidth and window.innerHeight 将canvas元素的高度和宽度设置为window.innerHeight / innerWidth,导致滚动条 - Setting height & width of canvas element to window.innerHeight/innerWidth causing scrollbars 不是“ window”和“ window.innerHeight”,而是 - Not “window” and not “window.innerHeight” but 如何使用sinon存根window.innerWidth / innerHeight? - How do I stub window.innerWidth/innerHeight with sinon? iOS Chrome 产生不正确的 window.innerWidth 和 innerHeight - iOS Chrome yields incorrect window.innerWidth and innerHeight JavaScript中的window.innerWidth - window.innerWidth in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM