简体   繁体   English

在网页上可见之前确定DIV高度的最佳方法是什么?

[英]what is the best way to determine a DIV's height before visible on the webpage?

I have a script that will fetch data from server and compose a DIV according to it. 我有一个脚本,它将从服务器获取数据并根据它编写DIV。 The DIV will contain image, text and other undetermined content. DIV将包含图像,文本和其他未确定的内容。 I want to arrange it freely, so I need to know its height and width before it is shown to the user. 我想自由地安排它,所以我需要知道它的高度和宽度才能显示给用户。

But per my understanding, I should ask browser to render it firstly, otherwise I can't know the width&height. 但根据我的理解,我应该先要求浏览器渲染它,否则我无法知道宽度和高度。

Is there any best way to know a DIV's width&height without showing it to user? 有没有最好的方法来了解DIV的宽度和高度而不向用户显示?

EDIT: 编辑:

Some good guy suggests that I use jquery's .width() method, but I am wondering that if the DIV contains image, will jquery wait after the image is downloaded? 一些好人建议我使用jquery的.width()方法,但我想知道如果DIV包含图像,jquery会在下载图像后等待吗? I don't know the image's dimension beforehand. 我事先并不知道图像的尺寸。

You can set display: none style on the div. 您可以在div上设置display: none style。 Then use .width() from jQuery and you will get this value. 然后从jQuery使用.width() ,你就会得到这个值。

For the duration of the calculation it will use .swap() to set style so element is visible, call callback calculating the width and swap style back. 在计算的持续时间内,它将使用.swap()来设置样式,以便元素可见,调用回调计算宽度和交换样式。

It will set (see the code ): 它将设置(参见代码 ):

  • position: absolute 位置:绝对
  • visiblity: hidden 可见性:隐藏
  • display: block 显示:块

EDIT (see comments): 编辑 (见评论):

If you are loading a image and you want to make sure the image is loaded before you calculate the width then you can attach width calculation as the onload handler like this: 如果要加载图像并且要在计算宽度之前确保加载图像,则可以将宽度计算作为onload处理程序附加,如下所示:

$('#someImg').load(function() { 
  // image is loaded, you can calculate the width
  console.log($('#test').width())); 
});

HERE is a working example. 是一个有效的例子。

jQuery is smart enough to calculate the dimensions, but only once injected into the DOM. jQuery足够聪明,可以计算维度,但只有一次注入DOM。

var html = "<span>My Content</span>";

//hide your element then insert it into the webpage
var html_el = $(html).hide();
$('body').append(html_el);

//get width and height
var width = html_el.width();
var height = html_el.height();

//Remove element from page if no longer needed
html_el.remove();

Also note, A block element <div> will be rendered at screen width, thus not correctly reporting the width of contained elements. 另请注意,块元素<div>将以屏幕宽度呈现,因此无法正确报告所包含元素的宽度。 To get around this you add these styles to your container: float:left or display:inline-block . 要解决此问题,请将这些样式添加到容器中: float:leftdisplay:inline-block Ex: 例如:

var html = "<div><h2>heading</h2><input type=\"text\" /></div>";

var html_el = $(html).css('float','left').hide();

set style="display:none"检查宽度,然后显示是否需要

If you set style.display to 'block' and style.visibility to 'hidden' the browser should calculate the sizes, but not show the div . 如果将style.display设置为'block'并将style.visibility'hidden'则浏览器计算大小,但不显示div

However, I don't know whether all browsers do this correctly. 但是,我不知道所有浏览器是否都能正确执行此操作。

The best that I can think of is to very quickly show it (might even be off-screen depending on your situation), then fetch its size, and then to hide it again. 我能想到的最好的就是快速显示它(甚至可能根据你的情况在屏幕外),然后获取它的大小,然后再次隐藏它。

But yes, if the div contains images, these need to be loaded first (or have a width / height set on them) before you know the size of the div container. 但是,是的,如果div包含图像,则在知道div容器的大小之前,需要首先加载这些图像(或者在它们上设置宽度/高度)。 You can use 您可以使用

$(document).load(function() { calculate height... })

to only calculate height after the complete page (including images) has been loaded. 仅在加载完整页面(包括图像)后计算高度。

I do something similar where the height and width are both unknown. 我做了类似的事情,高度和宽度都是未知的。 The data that populates the div with display:none is data from a snmp request so there is some time associated with getting and displaying that data. 使用display:none填充div的数据是来自snmp请求的数据,因此有一些时间与获取和显示该数据相关联。 Here is a little snip of code i use to adjust the absolute position based on the height. 这是我用来根据高度调整绝对位置的一小段代码。

var height = $('.stripPopupContainer', this).height();

    $('.stripPopupContainer', this).css({
        left: '-5px',
        top: '-'+ height + 'px'
    });

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

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