简体   繁体   English

使用CSS和JS动态设置div尺寸时,如何补偿更长的加载时间?

[英]How can I compensate for longer load times when dynamically setting div dimensions with CSS and JS?

I am creating a Polymer app which displays information in rows in a custom element "table". 我正在创建一个Polymer应用程序,它在自定义元素“table”中的行中显示信息。 I am using polymer flexbox classes to initially position elements. 我使用聚合物flexbox类来初始定位元素。 I need to take measurements with JS after the elements have loaded and assign widths to elements in the table accordingly. 我需要在元素加载后使用JS进行测量,并相应地为表中的元素指定宽度。 The problem goes something like this: 问题是这样的:

row 1
*--------------------*
|       *-----------*|
|       |    el a   ||
|       *-----------*|
*--------------------*
row 2
*--------------------*
|            *------*|
|            | el b ||
|            *------*|
*--------------------*

Row 1 and row 2 have fixed widths, but el a or b could be of any width. 第1行和第2行具有固定的宽度,但是el ab可以具有任何宽度。 Regardless of contents of el a or b , I want the el's contained in the rows to be all the same width as the widest. 无论el ab的内容如何,​​我都希望行中包含的el与宽度最宽。 So in this case, a is widest so el b should adjust its width to match a . 因此,在这种情况下, a是最宽的,因此el b应调整其宽度以匹配a

I am using polymers attached method to ensure the elements are loaded before taking scrollWidth measurements via JS. 我正在使用聚合物attached方法来确保在通过JS进行scrollWidth测量之前加载元素。 I initially ran into the problem of getting undefined elements when I tried to grab them with querySelector , but I fixed this using Polymer's async . 当我尝试用querySelector抓取它时,我最初遇到了获取未定义元素的问题,但我使用Polymer的async修复了这个问题。 I can style the elements fine in a modern browser just fine. 我可以在现代浏览器中很好地设计元素的样式。 Consider the following code. 请考虑以下代码。

attached: {
  this.async(function() {
    console.log(Polymer.dom(this.root).querySelector('#a').scrollWidth);
    console.log(Polymer.dom(this.root).querySelector('#b').scrollWidth);
  }, 100);
}

A modern version of chrome will return something like 100px for a and 60px for b . 现代版本的chrome将返回类似100px的a和60px的b If I test an older version of chrome or a different browser like firefox, a and b will be 5-15px less than what modern chrome measured. 如果我测试较旧版本的chrome或不同的浏览器(如firefox),则ab将比现代测量的镀铬低5-15px。 I found out that if I increase the async time enough, no matter the age of the browser, I will eventually get a measurement matching what modern chrome returned. 我发现如果我足够长时间地增加异步时间,无论浏览器的年龄如何,我最终都会得到一个与现代镀铬返回的测量值相匹配的测量值。 This is to say while it appears the div exists on the page and I can grab it with querySelector , it seems to be fluctuating so that it is not at its full width yet. 这就是说,当看起来div存在于页面上并且我可以使用querySelector抓取它时,它似乎是波动的,所以它还没有达到它的全宽。

I don't like guessing how long it will take for the elements to fully load before I can take measurements. 我不想猜测元素在进行测量之前需要多长时间才能完全加载。 I am trying to figure out a way I can be 100% confident that the elements are done loading or perhaps an alternate method of styling these elements I have overlooked. 我试图找出一种方法,我可以100%确信元素已经完成加载,或者可能是另一种造型这些我忽略的元素的方法。 I have had the most success with using setTimeout on the function that is doing the measuring and waiting for it to measure the same thing several times in a row. 我最成功的是在正在进行测量的函数上使用setTimeout并等待它连续几次测量同样的事情。 But even that has been inconsistent at times and occasionally b will be slightly less wide than a when the web page appears. 但即使这样,一直走在时间和偶尔不一致b将略少宽比a出现在网页时。

One option when you need to keep checking things, even if it's been true a couple times, is to continue to use setTimeout infinitely until the values are no longer changing for a certian # of iterations. 当你需要继续检查事物的一个选项,即使它已经成功几次,就是继续无限地使用setTimeout,直到值不再为certian#of iterations改变。 For example, if they are still the same after 10 timeouts, stop the timeouts from going all together. 例如,如果它们在10次超时后仍然相同,则停止超时。

something like... 就像是...

var count = 0;
function check() {
   do_something();
   if (same_as_last_time == true) {count++}
   if (count < 10) setTimeout (check,100);
}
setTimeout (check,100);

Although this could be wildly inefficient and possibly ineffective depending on how staggered your data is appearing. 虽然这可能非常低效,并且可能无效,具体取决于您的数据出现的交错方式。

I believe the actual solution here lies in the fact that you are using the variable ".scrollWidth" as your measurement point, which is a terrible measurement to standardize your widths across browsers. 我相信这里的实际解决方案在于您使用变量“.scrollWidth”作为测量点,这是一种可靠的测量方法,可以跨浏览器标准化您的宽度。 See this SO post for more info on that ( https://stackoverflow.com/a/33672058/3479741 ) 有关详细信息,请参阅此SO帖子( https://stackoverflow.com/a/33672058/3479741

I would recommend instead using a different method of acquiring the width and compare to see whether your results are more consistent. 我建议改为使用另一种获取宽度的方法,然后比较看看你的结果是否更一致。 There are many options that remain more consistent than ".scrollWidth" in the article above. 在上面的文章中,有许多选项比“.scrollWidth”更加一致。

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

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