简体   繁体   English

计算要在一个屏幕上显示的元素的宽度和高度,元素数量未知

[英]Calculating width and height of an element to be on one screen, unknown number of elements

I have project where I would like to display sensor readings from the building. 我有一个项目,我想显示建筑物中的传感器读数。 It contains temperature, date and name of the room. 它包含温度,日期和房间名称。 It is in one small simple square. 它在一个小的简单正方形中。 I would like it to be displayed on the TV monitor( I am unaware of what the resolution will be, but that can be calculated with JS). 我希望将其显示在电视监视器上(我不知道分辨率是多少,但是可以用JS计算出来)。

Problem is, I don't know how many readings there are going to be and I need to show them all on the screen(Since it is going to be on monitor without possibility to scroll). 问题是,我不知道会有多少读数,我需要将它们全部显示在屏幕上(因为它将一直在显示器上,无法滚动)。 I don't know how to calculate their dimension so they would fit. 我不知道如何计算尺寸,以适应尺寸。 I didn't find any miraculous css property for this so I am fine with creating some JS function. 我没有为此找到任何神奇的css属性,因此我可以创建一些JS函数。 However, I don't have an idea where to start. 但是,我不知道从哪里开始。 I would know the number of sensors and resolution, that is all. 我会知道传感器的数量和分辨率,仅此而已。 This number of readings will change over time though. 但是,该读数数量会随着时间而变化。

Any JS libraries that could do this? 有没有JS库可以做到这一点? If none, what function should I write? 如果没有,我应该写什么函数?

Edit: Touffy's solution works great! 编辑: Touffy的解决方案效果很好! I found this article for others in needs: Dynamic number of rows and columns with CSS Grid Layout and CSS variables 我为其他有需要的人找到了这篇文章: 具有CSS网格布局和CSS变量的行和列的动态数量

You can probably decide which browser will be used for this sort of application, so you can pick one that supports CSS Grid Layout . 您可能可以决定将哪种浏览器用于这种应用程序,因此可以选择一个支持CSS Grid Layout的浏览器 With that, you can do what you want without any JavaScript by specifying the grid container to take the whole viewport ( 100vh and 100vw ), and letting it arrange the grid items with the constraints you like (number of columns, spacing…). 这样,您可以通过指定网格容器来容纳整个视口( 100vh100vw ),并让它以您喜欢的约束(列数,间距……)排列网格项,而无需任何JavaScript来完成您想做的事情。

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

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