使用jQuery检测div的高度何时发生变化 [英] Detecting when a div's height changes using jQuery

查看:116
本文介绍了使用jQuery检测div的高度何时发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div包含一些动态添加和删除的内容,因此它的高度经常变化。我还有一个完全位于javascript下方的div,所以除非我能检测到div的高度何时发生变化,否则我无法重新定位它下面的div。



<那么,我如何检测该div的高度何时发生变化?我假设我需要使用一些jQuery事件,但我不确定要连接哪一个。

解决方案

使用从css-element-queries库调整传感器大小:



https: //github.com/marcj/css-element-queries

  new ResizeSensor(jQuery('#myElement') ),function(){
console.log('myelement has resized');
});

它使用基于事件的方法,不会浪费你的cpu时间。适用于所有浏览器。 IE7 +。


I've got a div that contains some content that's being added and removed dynamically, so its height is changing often. I also have a div that is absolutely positioned directly underneath with javascript, so unless I can detect when the height of the div changes, I can't reposition the div below it.

So, how can I detect when the height of that div changes? I assume there's some jQuery event I need to use, but I'm not sure which one to hook into.

解决方案

Use a resize sensor from the css-element-queries library:

https://github.com/marcj/css-element-queries

new ResizeSensor(jQuery('#myElement'), function() {
    console.log('myelement has been resized');
});

It uses a event based approach and doesn't waste your cpu time. Works in all browsers incl. IE7+.

这篇关于使用jQuery检测div的高度何时发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆