覆盖 jQuery 以在元素变得可见后执行代码 - 如何执行?
我正在开发一个小型的 javascript 框架供我们公司内部使用。在某些情况下,我们的框架会覆盖基本的 jQuery 功能,以便处理内部逻辑或修复浏览器错误等。我希望这些情况对我们的开发人员来说是完全透明的,这样他们就可以继续按照他们习惯的方式使用 jQuery。
我遇到了一个问题,我似乎无法以一种透明的方式解决这个问题 - 至少我无法想出一个好的方法 - 所以我转向你:)。
我想要做的是以某种方式覆盖 jQuery,这样我就可以在每次“变得可见”时执行一段代码。例如,如果开发人员运行 show() 方法,我想在元素变得可见后执行我的代码。事实上,无论开发人员如何“使元素可见”(例如 show()、css()、animate() 等),我希望我的代码能够运行,
我知道 show() 例如有一个“回调”参数就可以用于此目的。但重点是;这对于开发人员来说应该是完全透明的。他不需要知道这段代码是“在下面运行的”。
我所有的覆盖都是使用闭包完成的,因此我可以保存对原始方法的引用,做我的事情并执行原始方法。下面是我如何使用 show 方法执行此操作的示例:
(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);
我最初的方法是简单地这样做(在“// Do stuff after”处运行我的代码)。这也很有效,除非您传递速度参数(因为它在内部使用类似 setTimeout 或 setInterval 的东西),或者使用其他“显示元素”方法之一。
jQuery 中是否有一些主“显示元素”方法,我可以覆盖它,以便所有与“显示元素”相关的方法都会受到我的代码片段的影响?或者我是否必须覆盖所有“显示方法”?谁能举例说明我如何完成这项任务?以及我需要覆盖的内容(如果这是这样做的话)。
提前致谢。
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 “showing 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你想要类似 .is() 和 :visible 选择器的东西,听起来你可能想使用 setInterval
function checkElementVis(){
}运行这样的函数
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(){
}