设置画布的高度以覆盖整个可滚动页面(Setting height of canvas to cover entirety of scrollable page)

编程入门 行业动态 更新时间:2024-10-28 15:28:39
设置画布的高度以覆盖整个可滚动页面(Setting height of canvas to cover entirety of scrollable page)

我的canvas元素目前具有以下属性:

var c = document.getElementById("c"); var ctx = c.getContext("2d"); //making the canvas full screen c.height = window.innerHeight; c.width = window.innerWidth;

当我将画布放在一个内容超出屏幕高度的页面上时,当我向下滚动时画布会切断,这是可以预料的。 如何更改height属性以使画布延伸到页面上的内容?

My canvas element currently has the following attributes:

var c = document.getElementById("c"); var ctx = c.getContext("2d"); //making the canvas full screen c.height = window.innerHeight; c.width = window.innerWidth;

When I place the canvas on a page which has more content than can fit in the height of the screen, the canvas cuts off when I scroll down, which is to be expected. How do I change the height property to get the canvas to extend as far down as there is content on the page?

最满意答案

用这个:

var c = document.getElementById("c"); var ctx = c.getContext("2d"); //making the canvas full screen c.height = document.getElementsByTagName('body')[0].scrollHeight; c.width = window.innerWidth;

Use this:

var c = document.getElementById("c"); var ctx = c.getContext("2d"); //making the canvas full screen c.height = document.getElementsByTagName('body')[0].scrollHeight; c.width = window.innerWidth;

更多推荐

本文发布于:2023-08-06 19:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:画布   高度   页面   Setting   height

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!