Skip to content

Commit 6365a12

Browse files
committed
Update readme and tests for autoFocus
1 parent 8a981b4 commit 6365a12

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

readme.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ The following options, shown with their default values, are available for both `
4747
// one of 'top' or 'left'
4848
direction: 'top',
4949

50-
// only use if you want to override default behavior
50+
// only use if you want to override default behavior or if using $.smoothScroll
5151
scrollTarget: null,
5252

53+
// automatically focus the target element after scrolling to it (see readme for details)
54+
autoFocus: false,
55+
5356
// string to use as selector for event delegation
5457
delegateSelector: null,
5558

@@ -67,7 +70,7 @@ The following options, shown with their default values, are available for both `
6770

6871
// speed can be a number or 'auto'
6972
// if 'auto', the speed will be calculated based on the formula:
70-
// (current scroll position - target scroll position) / autoCoeffic
73+
// (current scroll position - target scroll position) / autoCoefficient
7174
speed: 400,
7275

7376
// autoCoefficent: Only used when speed set to "auto".
@@ -185,7 +188,12 @@ if (reSmooth.test(location.hash)) {
185188

186189
## Focus element after scrolling to it.
187190

188-
Imagine you have a link to a form somewhere on the same page. When the user clicks the link, you want the user to be able to begin interacting with that form. With the smoothScroll plugin, you can use the `afterScroll` callback function. Here is an example that focuses the first input within the form after scrolling to the form:
191+
Imagine you have a link to a form somewhere on the same page. When the user clicks the link, you want the user to be able to begin interacting with that form.
192+
193+
* As of **smoothScroll version 2.2**, the plugin will automatically focus the element if you set the `autoFocus` option to `true`.
194+
* In the future, versions 3.x and later will have `autoFocus` set to true **by default**.
195+
* If you are using the low-level `$.smoothScroll` method, `autoFocus` will only work if you've also provided a value for the `scrollTarget` option.
196+
* Prior to version 2.2, you can use the `afterScroll` callback function. Here is an example that focuses the first input within the form after scrolling to the form:
189197

190198
```js
191199
$('a.example').smoothScroll({
@@ -196,15 +204,18 @@ $('a.example').smoothScroll({
196204

197205
```
198206

199-
For accessibility reasons, it might make sense to focus any element you scroll to, even if it's not a natively focusable element. To do so, you could add a `tabIndex` attribute to the target element:
207+
For accessibility reasons, it might make sense to focus any element you scroll to, even if it's not a natively focusable element. To do so, you could add a `tabIndex` attribute to the target element (this, again, is for versions prior to 2.2):
200208

201209
```js
202-
$('a.example').smoothScroll({
210+
$('div.example').smoothScroll({
203211
afterScroll: function(options) {
204212
var $tgt = $(options.scrollTarget);
205-
$tgt.attr('tabIndex', '0');
206-
// Using $tgt[0] allows us to call .focus() on the DOM node itself, not the jQuery collection
207213
$tgt[0].focus();
214+
215+
if (!$tgt.is(document.activeElement)) {
216+
$tgt.attr('tabIndex', '-1');
217+
$tgt[0].focus();
218+
}
208219
}
209220
});
210221
```

test/tests.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ QUnit.test('Scrolls the #scrollable element, or not.', function(assert) {
6161
});
6262
});
6363

64+
QUnit.test('Auto-focuses the scroll target element after scrolling', function(assert) {
65+
var done = assert.async(1);
66+
67+
$.smoothScroll({
68+
scrollTarget: '#scrollable',
69+
speed: 100,
70+
autoFocus: true,
71+
afterScroll: function(opts) {
72+
assert.equal($(opts.scrollTarget)[0], document.activeElement, 'autofocus scrollTarget');
73+
done();
74+
}
75+
});
76+
});
77+
6478
QUnit.test('Scrolls the #scrollable element to absolute or relative scroll position', function(assert) {
6579
var done = assert.async(3);
6680

0 commit comments

Comments
 (0)