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

前端 未结 11 1127
南笙
南笙 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:37

    Interesting question. The problem is that height() does not accept a callback, so you wouldn't be able to fire up a callback. Use either animate() or css() to set the height and then trigger the custom event in the callback. Here is an example using animate() , tested and works (demo), as a proof of concept :

    $('#test').bind('style', function() {
        alert($(this).css('height'));
    });
    
    $('#test').animate({height: 100},function(){
    $(this).trigger('style');
    }); 
    

提交回复
热议问题