Skip to content

Commit 8a981b4

Browse files
committed
Add autoFocus option
1 parent d73ec93 commit 8a981b4

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/jquery.smooth-scroll.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
// only use if you want to override default behavior
2121
scrollTarget: null,
2222

23+
// automatically focus the target element after scrolling to it
24+
autoFocus: false,
25+
2326
// fn(opts) function to be called before scrolling occurs.
2427
// `this` is the element(s) being scrolled
2528
beforeScroll: function() {},
@@ -106,6 +109,7 @@
106109
};
107110

108111
var rRelative = /^([\-\+]=)(\d+)/;
112+
109113
$.fn.extend({
110114
scrollable: function(dir) {
111115
var scrl = getScrollable.call(this, {dir: dir});
@@ -218,6 +222,21 @@
218222
return explicit;
219223
};
220224

225+
var onAfterScroll = function(opts) {
226+
var $tgt = $(opts.scrollTarget);
227+
228+
if (opts.autoFocus && $tgt.length) {
229+
$tgt[0].focus();
230+
231+
if (!$tgt.is(document.activeElement)) {
232+
$tgt.prop({tabIndex: -1});
233+
$tgt[0].focus();
234+
}
235+
}
236+
237+
opts.afterScroll.call(opts.link, opts);
238+
};
239+
221240
$.smoothScroll = function(options, px) {
222241
if (options === 'options' && typeof px === 'object') {
223242
return $.extend(optionOverrides, px);
@@ -288,7 +307,7 @@
288307
duration: speed,
289308
easing: opts.easing,
290309
complete: function() {
291-
opts.afterScroll.call(opts.link, opts);
310+
onAfterScroll(opts);
292311
}
293312
};
294313

@@ -299,7 +318,7 @@
299318
if ($scroller.length) {
300319
$scroller.stop().animate(aniProps, aniOpts);
301320
} else {
302-
opts.afterScroll.call(opts.link, opts);
321+
onAfterScroll(opts);
303322
}
304323
};
305324

0 commit comments

Comments
 (0)