简体   繁体   English

将画布拉伸至全宽和全高以动态调整尺寸

[英]Stretch canvas to full width and height for dynamic resize

I have a page where elements are dynamically added below each other (and can be dragged around). 我有一个页面,其中元素彼此之间动态添加(可以拖动)。 When the number of elements exceed the viewport height, the height of the page gets longer. 当元素数量超过视口高度时,页面的高度会变长。 Same with the user dragging it out of normal bounds--it gets wider. 与用户将其拖出正常范围一样-它变得更宽。

What is the event that I can track this with? 我可以追踪的事件是什么? $(window).resize() does not work because the window isn't really resizing. $(window).resize()不起作用,因为窗口的大小实际上没有调整。 Is there a single event I can handle, instead of doing something like checking the width and height whenever an element is dragged or added? 我是否可以处理一个事件,而不是在拖动或添加元素时进行诸如检查宽度和高度的操作?

Fiddle of what I'm talking about . 我在说什么

Inside your click event, you want to re-set the height to the document height. 在单击事件内部,您想将高度重新设置为文档高度。

canvas.height = $(document).height();

See Fiddle 见小提琴


Alternatively you can: 或者,您可以:

$(document).bind('DOMSubtreeModified', function(e) {
    canvas.height = $(document).height();
});

But this is not advised as it will set the height almost every time something happens to the DOM. 但是不建议这样做,因为它将几乎在每次DOM发生任何事情时都设置高度。 You can imagine how excessive that is. 您可以想象那是多么的过分。 So it's better to call a function on the events you know would be expanding the content of the page. 因此,最好在您知道会扩展页面内容的事件上调用一个函数。

Also note that this may not work in IE, for that you might want to create your own function to check the height of the document on some interval (eg, 50ms). 另请注意,这可能在IE中不起作用,因为您可能希望创建自己的函数以一定间隔(例如50ms)检查文档的高度。

See Fiddle 见小提琴

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

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