开发者

Overwrite jQuery to execute code after element has become visible - how to?

I am developing a small javascript framework for internal use in our company. In some cases our framework is overwriting basic jQuery functionality in order to handle internal logic or fix browser bugs etc. I want these cases to be completely transparent for our developers so they can just keep using jQuery the way they are used to.

I have come across a problem that I can’t seem to get my head around to do in a nice transparent way – at least I can’t come up with a good way to do it - so I turn to you :).

What I want to do is overwrite jQuery in a way so I can execute a certain piece of code each time something “becomes visible”. For instance if a developer runs the show() method, I want to execute my code after the element has become visible. In fact no matter how the developer “makes an element visible” (e.g. show(), css(), animate() etc.) I want my code to run

I know that show() for instance has a “callback” parameter that can be used for just that. But the point is; this should be totally transparent for the developer. He does not need to know that this code is “run underneath”.

All my overwrites is done using closures, so I can save a reference to the original method, do my thing and execute the original. Here is an example of how I am doing this with the show method:

(function ($) {
  var originalShowMethod = jQuery.fn.show;
  jQuery.fn.show = function (speed, easing, callback) {
    // Do stuff before
    var theReturn = jQuery(originalShowMethod.call(this, speed, easing, callback));
    // Do stuff after
    return theReturn;
  };
})(jQuery);

My initial approach was to just simply do it this way (running my code at “// Do stuff after”). That also works great except when you are passing a speed parameter (because then it's using something like setTimeout or setInterval internally), or using one of the other “show an element” methods.

Is there some master “show an element” method in jQuery that I can overwrite so all methods that has something to do with “s开发者_开发技巧howing elements” will be affected by my piece of code? Or do I have to overwrite all "showing methods"? Can anyone give an example of how I can accomplice this task? and what i need to overwrite if that's the way to do it.

Thanks in advance.


I think you want something like the .is() and the :visible selector, it sounds like you might want to run a function like this using setInterval

function checkElementVis(){

if($(".element").is(":visible")){
    alert("is visible");
} else {
alert("not visible");
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜