[英]How can I detect when a certain DIV has changed size without polling? I.e., I want a resize event listener?
So I am running ads on a website that I have and one of the ads periodically changes the dimensions of the DIV that it is in -- it grows and shrinks and shows bouncing things and flashing lights and sparkles, etc. This is fantastic and I love it. 因此,我在自己拥有的网站上投放广告,其中一则广告会定期更改其所在DIV的尺寸-它会不断扩大和缩小,并显示弹跳的东西以及闪烁的灯光和闪光等。这真是太棒了,我爱它。
One problem, however, is that the layout of other elements on my page is screwed up when this happens. 但是,一个问题是,发生这种情况时,我页面上其他元素的布局被搞砸了。
This is no big deal though -- all I have to do is add an event listener to the DIV element in question and listen for resizing events. 不过,这没什么大不了的-我要做的就是将一个事件侦听器添加到有问题的DIV元素中,并侦听调整大小的事件。 When they occur, I will run a callback function that will adjust the layout of the other elements of my site.
当它们发生时,我将运行一个回调函数,该函数将调整网站其他元素的布局。
My problem is that the resize
event listener is only available for the window object itself, not for other elements in the DOM. 我的问题是,
resize
事件侦听器仅适用于窗口对象本身,而不适用于DOM中的其他元素。 How can I listen for resizing events on a particular DIV without polling? 如何在不轮询的情况下侦听特定DIV上的事件大小调整?
I would like to do something like: 我想做类似的事情:
$('my-div-id').on('resize', function() {
do_things();
adjust_layout();
etc();
});
You could use a window.setInterval, that checks every x ms if the element in question has changed size: 您可以使用window.setInterval,如果有问题的元素的大小已更改,则每隔x ms检查一次:
var ElementSize = $("#element").width();
window.setInterval(function(){
if( $("#element").width() != ElementSize )
{
ElementSize = $("#element").width();
redolayout();
}
}, 50);
function redolayout()
{
// Add code here to resize other elements according to the change.
}
Another option: https://github.com/sdecima/javascript-detect-element-resize 另一个选择: https : //github.com/sdecima/javascript-detect-element-resize
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.