Bra*_*ney 5 jquery jquery-plugins
我正在编写一个jQuery插件,它涉及将事件绑定到window.scroll.window.scroll中执行的操作取决于调用原始初始化时传入的设置.
如何在绑定事件中访问数据元素?
(function($) {
var methods = {
init : function(options) {
return this.each(function() {
$(window).bind("scroll.myPlugin", methods.windowOnScroll);
});
},
windowOnScroll : function() {
var $this = $(this);
var data = $this.data("scrollingLoader");
if (data.something) {
// ...
}
}
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
jQuery 提供了一个方便的函数 ,$.proxy它可以进行跨浏览器函数绑定。
(function($) {
var methods = {
init : function(options) {
return this.each(function() {
$(window).bind("scroll.myPlugin", $.proxy(methods.windowOnScroll,methods));
});
},
windowOnScroll : function() {
var $this = $(this);
var data = $this.data("scrollingLoader");
if (data.something) {
// ...
}
}
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
$.proxy 函数返回一个函数,该函数将始终在第二个参数的上下文中执行第一个参数中传递的函数。http://api.jquery.com/jQuery.proxy
| 归档时间: |
|
| 查看次数: |
563 次 |
| 最近记录: |