Attempting to trigger before & after events around jQuery show()

后端 未结 3 648
悲哀的现实
悲哀的现实 2021-02-15 02:02

I\'m trying to get a piece of our JavaScript run after some 3rd party code makes a modal dialog appear. I saw a pretty neat idea of hijacking the jQuery show function, but unfor

3条回答
  •  臣服心动
    2021-02-15 02:51

    I haven't tested anything, but sharing a few things I know

    1. show has an optional second parameter - easing. jQuery automatically detects this, but for a perfect a solution you have to consider this.

      _oldShow.apply(obj, [speed, null, newCallback]); 
      
    2. A callback makes sense only when there is a delay, that means value for speed. I hope your afterShow event will be triggered if there is a delay. If there is no delay you can trigger it after invoking _oldShow, like this

      _oldShow.apply(obj, [speed, null, newCallback]); 
      if(!speed){
        obj.trigger('afterShow');
      }
      

    EDIT:

    I tired a few things and frpm what I learned I would suggest you to create a new show function instead of overriding.

    jQuery.fn.extend({
    
        show2:function( speed, easing, callback ) {
            this.trigger('beforeShow');
            this.show(speed, easing, function(){
                $(this).trigger('afterShow');
                if ($.isFunction(callback)) {
                    callback.apply(this);
                }
            });
            if(!speed){
                this.trigger('afterShow');            
            }          
        }
    
    
    });
    

    demo: http://jsfiddle.net/9F8ea/2/

提交回复
热议问题