当一些dom元素改变高度时的jQuery事件

its*_*sme 5 javascript jquery dom javascript-events

当某些(任何人)元素改变自己的高度时,有没有办法设置jquery事件?

我真的不知道怎么能解雇这个

谢谢大家都会帮助我

喜欢:

$('body *').onHeightChange(function(){

//do somenthing
});
Run Code Online (Sandbox Code Playgroud)

use*_*654 9

每当您执行更改元素高度的操作时,都会触发事件.

$("#somelement").slideDown().trigger("heightchange");
Run Code Online (Sandbox Code Playgroud)

现在你可以绑定到:

$("body").on("heightchange",function(e){
    var $el = $(e.target); // the element that has a new height.
});
Run Code Online (Sandbox Code Playgroud)

你甚至可以monkeypatch许多可以改变高度的jQuery方法,让他们在使用方法之前和之后测试高度并相应地触发事件.下面是一个未经测试的例子

var topatch = ["slideup","slidedown","height","width","attr","css","animate"];
$.each(topatch,function(i,method){
    var oldfn = $.fn[method];
    $.fn[method] = function(){
        return this.each(function(){
            var $this = $(this), currHeight = $this.css("height"),
                result = oldfn.apply($this,arguments); 
            // check for promise object to deal with animations
            if ( $.isFunction(result.promise) ) {
                result.promise().done(function(){
                    if ( currHeight != $this.css("height") ) {
                        $this.trigger("heightchange");
                    }
                });
                return;
            }
            if ( currHeight != $this.css("height") ) {
                $this.trigger("heightchange");
            }            
        });
    };
});?
Run Code Online (Sandbox Code Playgroud)