简体   繁体   English

如何在不轮询的情况下检测某个DIV的大小已更改? 即,我想要调整大小的事件监听器?

[英]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.

相关问题 如何检测图像何时在浏览器中完成渲染(即绘制)? - How to detect when an image has finished rendering in the browser (i.e. painted)? 当检测到表单值已更改时,如何防止发生事件? - How can I prevent an event when detect the form values has changed? 如何动态更新我的CSS,即我想让用户选择字体大小,bckground颜色 - How can I dynamically update my CSS i.e. I want to give user an option of selecting font size, bckground color 当div的内容发生变化时,如何自动调整div的大小? - How do I resize a div automatically to the size of its contents when the contents of the div have changed? 如何检查Kendo UI ComboBox是否脏(即值已从默认值更改)? - How to check if Kendo UI ComboBox is Dirty (i.e. value has been changed from default value)? 如何在 JavaScript 中附加窗口调整大小事件侦听器? - How can I attach a window resize event listener in JavaScript? javascript是否可以检测滚动条何时不可用(即在移动浏览器上)? - Can javascript detect when scrollbars are unavailable (i.e. on mobile browsers)? 当屏幕尺寸为 Ie large 时派遣到 redux - dispatch to redux when screen size is for I.e. large 如何将事件侦听器附加到本身已使用Javascript插入的元素上? (没有jQuery) - How can I attach an event listener to an element that has itself been inserted with Javascript? (without jQuery) 如何获得屏幕的物理尺寸(即英寸)? - How to get screen's physical size (i.e. in inches)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM