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

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

    There is no inbuilt support for the style change event in jQuery or in java script. But jQuery supports to create custom event and listen to it but every time there is a change, you should have a way to trigger it on yourself. So it will not be a complete solution.

    0 讨论(0)
  • 2020-11-22 08:31

    As others have suggested, if you have control over whatever code is changing the style of the element you could fire a custom event when you change the element's height:

    $('#blah').bind('height-changed',function(){...});
    ...
    $('#blah').css({height:'100px'});
    $('#blah').trigger('height-changed');
    

    Otherwise, although pretty resource-intensive, you could set a timer to periodically check for changes to the element's height...

    0 讨论(0)
  • 2020-11-22 08:31

    you can try Jquery plugin , it trigger events when css is change and its easy to use

    http://meetselva.github.io/#gist-section-attrchangeExtension
    
     $([selector]).attrchange({
      trackValues: true, 
      callback: function (e) {
        //console.log( '<p>Attribute <b>' + e.attributeName +'</b> changed from <b>' + e.oldValue +'</b> to <b>' + e.newValue +'</b></p>');
        //event.attributeName - Attribute Name
        //event.oldValue - Prev Value
        //event.newValue - New Value
      }
    });
    
    0 讨论(0)
  • 2020-11-22 08:33

    Since jQuery is open-source, I would guess that you could tweak the css function to call a function of your choice every time it is invoked (passing the jQuery object). Of course, you'll want to scour the jQuery code to make sure there is nothing else it uses internally to set CSS properties. Ideally, you'd want to write a separate plugin for jQuery so that it does not interfere with the jQuery library itself, but you'll have to decide whether or not that is feasible for your project.

    0 讨论(0)
  • 2020-11-22 08:33

    The declaration of your event object has to be inside your new css function. Otherwise the event can only be fired once.

    (function() {
        orig = $.fn.css;
        $.fn.css = function() {
            var ev = new $.Event('style');
            orig.apply(this, arguments);
            $(this).trigger(ev);
        }
    })();
    
    0 讨论(0)
  • 2020-11-22 08:35

    How about jQuery cssHooks?

    Maybe I do not understand the question, but what you are searching for is easily done with cssHooks, without changing css() function.

    copy from documentation:

    (function( $ ) {
    
    // First, check to see if cssHooks are supported
    if ( !$.cssHooks ) {
      // If not, output an error message
      throw( new Error( "jQuery 1.4.3 or above is required for this plugin to work" ) );
    }
    
    // Wrap in a document ready call, because jQuery writes
    // cssHooks at this time and will blow away your functions
    // if they exist.
    $(function () {
      $.cssHooks[ "someCSSProp" ] = {
        get: function( elem, computed, extra ) {
          // Handle getting the CSS property
        },
        set: function( elem, value ) {
          // Handle setting the CSS value
        }
      };
    });
    
    })( jQuery ); 
    

    https://api.jquery.com/jQuery.cssHooks/

    0 讨论(0)
提交回复
热议问题