简体   繁体   English

div调整大小后如何检测html div的宽度和高度

[英]How to detect html div width and height once div is done resizing

I am working with two divs where, one in the left is a collapsible container and the one in the right is a main container, something like this: 我正在与两个div一起工作,其中,左边的一个是可折叠的容器,右边的一个是主容器,如下所示:

================================
|        |                     |
|        |                     |
|        |                     |
|  div 1 |         div 2       | 
|        |                     |
|        |                     | 
================================

I am collapsing div 1 using simple css class and when div 1 is not visible, div 2 expands in size and takes over rest of the space. 我正在使用简单的CSS类折叠div 1,并且当div 1不可见时,div 2会扩展大小并占据其余空间。 Now I need to get the size of the div 2 once it is done with the resizing (which is not instant and it is using transition). 现在,一旦调整大小后,它就需要获得div 2的大小(它不是即时的,它正在使用过渡)。

I want to render d3 stuff in div 2 and need to to know the correct dimension of the div. 我想在div 2中渲染d3内容,并且需要知道div的正确尺寸。 Unfortunately, 不幸,

$("#div2-id").width() $( “#DIV2-ID”)。宽度()

is returning undefined result. 返回不确定的结果。 I think it is mainly due to the transition. 我认为这主要是由于过渡。 How can I get correct width and height of the div after resizing is done? 调整大小后如何获得正确的div宽度和高度?

I'd try using a timeout with the duration of the transition. 我会尝试在过渡期间使用超时。

var delay = 2000;
var t = setTimeout(function(){
    var width = $("#div2-id").width();
    clearTimeout(t);
}, delay);

The delay is in milliseconds, so this assumes your CSS transition is 2s . delay以毫秒为单位,因此假设您的CSS过渡为2s

Read this post as I believe is a more accurate approach, as you fire an event as soon as the transition ends. 阅读这篇文章,因为我相信这是一种更准确的方法,因为在转换结束后立即触发一个事件。 Callback when CSS3 transition finishes CSS3过渡完成时回调

The transitionend Event 过渡事件

There is an event called transitionend. 有一个事件称为Transitionend。 Adding a handler to this event will trigger any action after your transition ends instead of using a delay function 向此事件添加处理程序将转换结束触发任何操作而不是使用延迟函数

example click on the div box that has the text "left" inside.. when the transition ends the width of the div box that has the text "right" will pop up 例如,单击内部具有文本“ left”的div框。当过渡结束时,将弹出具有文本“ right”的div框的宽度

 function handler(){ if(window.getComputedStyle){ alert(window.getComputedStyle(document.getElementById('right'), 'width').width) } else {document.getElementById('right').currentStyle.width} } function click(){ document.getElementById('left').style.width='0px' } document.getElementById('left').addEventListener("transitionend",handler); document.getElementById('left').addEventListener('click',click,false); 
 #left{ float:left; transition-property:width; transition-duration:3s; border:solid; width:100px; } #right{ border:solid; overflow:hidden; } 
 <div> <div id="left"> left </div> <div id="right"> ddd </div> </div> 

Note Beware 注意当心

this event is only supported browser versions here http://www.w3schools.com/jsref/event_transitionend.asp 此事件仅受支持的浏览器版本在此处http://www.w3schools.com/jsref/event_transitionend.asp

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

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