为什么 window.innerWidth 是错误的?
我正在尝试制作帆布JavaScript游戏,所以我在代码中添加了
window.addEventListener("resize", () => {
console.log("resi2ze");
canvasElement.width = window.innerWidth;
canvasElement.height = window.innerHeight;
});
I'm trying to make a canvas JavaScript game, so i added this in code
window.addEventListener("resize", () => {
console.log("resi2ze");
canvasElement.width = window.innerWidth;
canvasElement.height = window.innerHeight;
});
But when I resize browser window, canvas height is bigger than body height
(for example, body height is 465px, and canvas height is 462px). How can I prevent it, so canvas height would be same as window height?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来滚动条本身就是一个问题,因为它添加了很少的像素。
我总是将
overflow:hidden
添加到正文,这样滚动条就不会显示。Seems like the scrollbar itself is an issue, because it adds few pixels.
I always add
overflow: hidden
to body so scrollbar doesn't show up.