Is it possible to listen to a “style change” event?

前端 未结 11 1109
南笙
南笙 2020-11-22 08:03

Is it possible to create an event listener in jQuery that can be bound to any style changes? For example, if I want to \"do\" something when an element changes dimensions, o

11条回答
  •  抹茶落季
    2020-11-22 08:41

    Just adding and formalizing @David 's solution from above:

    Note that jQuery functions are chainable and return 'this' so that multiple invocations can be called one after the other (e.g $container.css("overflow", "hidden").css("outline", 0);).

    So the improved code should be:

    (function() {
        var ev = new $.Event('style'),
            orig = $.fn.css;
        $.fn.css = function() {
            var ret = orig.apply(this, arguments);
            $(this).trigger(ev);
            return ret; // must include this
        }
    })();
    

提交回复
热议问题