jquery:有没有办法在渲染之前猜测高度?

发布于 2024-12-12 21:02:49 字数 697 浏览 0 评论 0原文

这是我的代码:

  • 首先,我分配一些 htm 代码,其中有一个表格。
  • (表格不可见)
  • 然后我转储高度:每行都是 0。
  • 然后我 fadeIn() 表格,
  • 然后转储高度:每行为 320px。

你知道我如何在渲染之前计算高度吗?

我的目标是,如果行的高度太大,则“剪切”行中的一些信息。

  $('#tableau').html(htm);
  $('.tableau tr td').each(function() {
    console.log($(this).height());
  } );
  $('#wait').queue(function() {
    $('#tableau').fadeIn(100).queue(function() {
        $('.tableau tr td').each(function() { console.log($(this).height()); } );
      }   
    );  
  });

我知道这听起来像这里但这不是完全相同的问题。

谢谢你!

Here's my code:

  • First I assign some htm code where there's a table in it.
  • (the table is not visible)
  • then I dump the height: it is 0 for each row.
  • then I fadeIn() the table
  • then I dump the height: it is 320px for each row.

Have you any idea how I could do to compute height before rendering?

My goal is to "cut" some information in the rows if their height is too big.

  $('#tableau').html(htm);
  $('.tableau tr td').each(function() {
    console.log($(this).height());
  } );
  $('#wait').queue(function() {
    $('#tableau').fadeIn(100).queue(function() {
        $('.tableau tr td').each(function() { console.log($(this).height()); } );
      }   
    );  
  });

I know it sounds like here but it's not exactly the same problem.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉墨 2024-12-19 21:02:49

在渲染之前不可能获取高度。您有三个选择:

  1. 一次渲染所有元素,然后获取高度。
  2. 追加一个元素,检查高度,并假设每个连续元素的高度相同。
  3. 一种非常不同的方法:将 max-height: 100px (示例)添加到元素的选择器中。

It's not possible to get the height before rendering. You've got three options:

  1. Render all elements at once, then get the height.
  2. Append one element, check the height, and assume this height to be the same for each consecutive element.
  3. A very different approach: Add max-height: 100px (example) to the selector of your element.
可可 2024-12-19 21:02:49

您可以尝试将内容渲染到屏幕外,使其不可见。因此将其放在绝对定位的div中,并将其向左移动10,000像素。然后渲染,并检查每个的高度。内容仍在渲染中,因此不完全满足您的要求,但它不会可见。然后您可以进行调整,最后将内容移动到屏幕上。

You might try rending the content offscreen, where it's not visible. So put it in a div that is positioned absolutely, and move it left 10,000 pixels. Then render, and check the height of each. The content is still being rendered so it doesn't fully meet your requirement, but it won't be visible. You can then adjust, and finally move the content onto the screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文