Skip to content

Commit 9f34a80

Browse files
koenpuntkswedberg
authored andcommitted
Add optional support for event delegation
1 parent e381940 commit 9f34a80

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/jquery.smooth-scroll.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
optionOverrides = {},
44
defaults = {
55
exclude: [],
6-
excludeWithin:[],
6+
excludeWithin: [],
77
offset: 0,
88

99
// one of 'top' or 'left'
1010
direction: 'top',
1111

12+
// if set, bind click events through delegation
13+
// supported since jQuery 1.4.2
14+
delegateSelector: null,
15+
1216
// jQuery set of elements you wish to scroll (for $.smoothScroll).
1317
// if null (default), $('html, body').firstScrollable() is used.
1418
scrollElement: null,
@@ -113,9 +117,7 @@
113117
var opts = $.extend({}, $.fn.smoothScroll.defaults, options),
114118
locationPath = $.smoothScroll.filterPath(location.pathname);
115119

116-
this
117-
.unbind('click.smoothscroll')
118-
.bind('click.smoothscroll', function(event) {
120+
var clickHandler = function(event) {
119121
var link = this,
120122
$link = $(this),
121123
thisOpts = $.extend({}, opts, $link.data('ssOpts') || {}),
@@ -156,7 +158,17 @@
156158

157159
$.smoothScroll( clickOpts );
158160
}
159-
});
161+
};
162+
163+
if (options.delegateSelector !== null) {
164+
this
165+
.undelegate(options.delegateSelector, 'click.smoothscroll')
166+
.delegate(options.delegateSelector, 'click.smoothscroll', clickHandler);
167+
} else {
168+
this
169+
.unbind('click.smoothscroll')
170+
.bind('click.smoothscroll', clickHandler);
171+
}
160172

161173
return this;
162174
}

0 commit comments

Comments
 (0)