Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event after applied CSS rules on elements in jQuery/Javascript

Tags:

jquery

dom

css

load

Is there an event after CSS rules applied to all elements in the DOM ? i know that the binding $(window).load(), from jQuery, is fired when all js and css files are loaded.

But not when they are applied (there is a small delay of some milliseconds between applying and inlcuding dynamically, such as: $('#design').attr('href', 'style.css') // design is an <link href="otherstyles.css">).

like image 352
e382df99a7950919789725ceeec126 Avatar asked Sep 11 '26 21:09

e382df99a7950919789725ceeec126


2 Answers

As per here, you could grab the contents of the CSS file with AJAX and then use jQuery's built in complete call to tell when the CSS has loaded.

like image 54
Elliot Bonneville Avatar answered Sep 14 '26 13:09

Elliot Bonneville


Such an event does not exist.

  • For all attribute changes, you can listen to the DOMAttrModifed mutation event.
  • Another option is to monitor specific style properties in an periodic function (interval),
  • Another method is to modify the jQuery.fn.css method, to intercept style changes via the jQuery API. This assumes that all relevant CSS property updates are done via jQuery.

    (function($){
        var $css = $.fn.css;
        $.fn.css = function(a,c) {
            if (c == null) return $css.call(this, a);
    
            return this.each(function() {
                var $this = $(this);
                // Your function logic...
                // ...
                // Call original method
                $css.call($this, a, c);
            });
        };
    })(jQuery);
    
like image 25
Rob W Avatar answered Sep 14 '26 15:09

Rob W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!