From f0de324ec7cba7bde73f48e5c673f7b9cf6ef4c6 Mon Sep 17 00:00:00 2001
From: Karl Swedberg Using npm: The old-fashioned way: Go to the following URL in your browser and copy/paste the code into your own file:
@@ -98,7 +98,7 @@ The following options, shown with their default values, are available for both The options object for Doesn't automatically fire, so you need to bind it to some other user
interaction. For example: The The following option, in addition to those listed for If you want to scroll to an element when the page loads, use For example, let's say you want to smooth scroll to Then for page-2.html, your script would do this: 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 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 Smooth Scroll Plugin
Download
npm
install
jquery-smooth-scroll
npm
install
jquery-smooth-scroll
$.fn.smoothScroll
Options
$.fn.smoothScroll
and $.smoothScroll
:{
offset: 0,
// one of 'top' or 'left'
direction:
'top'
,
// only use if you want to override default behavior
scrollTarget:
null
,
// string to use as selector for event delegation (Requires jQuery >=1.4.2)
delegateSelector:
null
,
// fn(opts) function to be called before scrolling occurs.
// `this` is the element(s) being scrolled
beforeScroll:
function
() {},
// fn(opts) function to be called after scrolling occurs.
// `this` is the triggering element
afterScroll:
function
() {},
easing:
'swing'
,
// speed can be a number or 'auto'
// if 'auto', the speed will be calculated based on the formula:
// (current scroll position - target scroll position) / autoCoeffic
speed: 400,
// autoCoefficent: Only used when speed set to "auto".
// The higher this number, the faster the scroll speed
autoCoefficient: 2,
// $.fn.smoothScroll only: whether to prevent the default click action
preventDefault:
true
}
{
offset: 0,
// one of 'top' or 'left'
direction:
'top'
,
// only use if you want to override default behavior
scrollTarget:
null
,
// string to use as selector for event delegation (Requires jQuery >=1.4.2)
delegateSelector:
null
,
// fn(opts) function to be called before scrolling occurs.
// `this` is the element(s) being scrolled
beforeScroll:
function
() {},
// fn(opts) function to be called after scrolling occurs.
// `this` is the triggering element
afterScroll:
function
() {},
easing:
'swing'
,
// speed can be a number or 'auto'
// if 'auto', the speed will be calculated based on the formula:
// (current scroll position - target scroll position) / autoCoeffic
speed: 400,
// autoCoefficent: Only used when speed set to "auto".
// The higher this number, the faster the scroll speed
autoCoefficient: 2,
// $.fn.smoothScroll only: whether to prevent the default click action
preventDefault:
true
}
$.fn.smoothScroll
can take two additional properties:
exclude
and excludeWithin
. The value for both of these is an array of
@@ -115,7 +115,7 @@ $.smoothScroll
document.body
)
$(
'button.scrollsomething'
).on(
'click'
,
function
() {
$.smoothScroll({
scrollElement: $(
'div.scrollme'
),
scrollTarget:
'#findme'
});
return
false
;
});
$(
'button.scrollsomething'
).on(
'click'
,
function
() {
$.smoothScroll({
scrollElement: $(
'div.scrollme'
),
scrollTarget:
'#findme'
});
return
false
;
});
$.smoothScroll
method can take one or two arguments.
@@ -129,7 +129,7 @@
$.smoothScroll
Additional Option
$.fn.smoothScroll
above, is available
for $.smoothScroll
:{
// jQuery set of elements you wish to scroll.
// if null (default), $('html, body').firstScrollable() is used.
scrollElement:
null
}
{
// jQuery set of elements you wish to scroll.
// if null (default), $('html, body').firstScrollable() is used.
scrollElement:
null
}
$.fn.scrollable
@@ -148,6 +148,22 @@
$.fn.firstScrollable
$('html, body').firstScrollable().animate({scrollTop: someNumber},
someSpeed)
Examples
+Smooth scrolling on page load
+$.smoothScroll()
in a script at the end of the body or use $(document).ready()
. To prevent the browser from automatically scrolling to the element on its own, your link on page 1 will need to include a fragment identifier that does not match an element id on page 2. To ensure that users without JavaScript get to the same element, you should modify the link's hash on page 1 with JavaScript. Your script on page 2 will then modify it back to the correct one when you call $.smoothScroll()
. <div id="scrolltome"></div>
on page-2.html. For page-1.html, your script might do the following:
+$(
'a[href="page-2.html#scrolltome"]'
).attr(
'href'
,
function
() {
var
hrefParts =
this
.href.split(/
#/);
hrefParts[1] =
'smoothScroll'
+ hrefParts[1];
return
hrefParts.join(
'#'
);
});
+// Call $.smoothScroll if location.hash starts with "#smoothScroll"
var
reSmooth = /^
#smoothScroll/;
var
id;
if
(reSmooth.test(location.hash)) {
// Strip the "#smoothScroll" part off (and put "#" back on the beginning)
id =
'#'
+ location.hash.replace(reSmooth,
''
);
$.smoothScroll({scrollTarget: id});
}
Focus element after scrolling to it.
+afterScroll
callback function. Here is an example that focuses the first input within the form after scrolling to the form:
+$(
'a.example'
).smoothScroll({
afterScroll:
function
(options) {
$(options.scrollTarget).find(
'input'
)[0].focus();
}
});
tabIndex
attribute to the target element:$(
'a.example'
).smoothScroll({
afterScroll:
function
(options) {
var
$tgt = $(options.scrollTarget);
$tgt.attr(
'tabIndex'
,
'0'
);
// Using $tgt[0] allows us to call .focus() on the DOM node itself, not the jQuery collection
$tgt[0].focus();
}
});
Notes
$.fn.smoothScroll
method looks
diff --git a/readme.md b/readme.md
index 38baeb2..07c0976 100644
--- a/readme.md
+++ b/readme.md
@@ -138,6 +138,62 @@ for `$.smoothScroll`:
`$('html, body').firstScrollable().animate({scrollTop: someNumber},
someSpeed)`
+## Examples
+
+### Smooth scrolling on page load
+
+If you want to scroll to an element when the page loads, use `$.smoothScroll()` in a script at the end of the body or use `$(document).ready()`. To prevent the browser from automatically scrolling to the element on its own, your link on page 1 will need to include a fragment identifier that does _not_ match an element id on page 2. To ensure that users without JavaScript get to the same element, you should modify the link's hash on page 1 with JavaScript. Your script on page 2 will then modify it back to the correct one when you call `$.smoothScroll()`.
+
+For example, let's say you want to smooth scroll to `` on page-2.html. For page-1.html, your script might do the following:
+
+```js
+$('a[href="page-2.html#scrolltome"]').attr('href', function() {
+ var hrefParts = this.href.split(/#/);
+ hrefParts[1] = 'smoothScroll' + hrefParts[1];
+ return hrefParts.join('#');
+});
+
+```
+
+Then for page-2.html, your script would do this:
+
+```js
+// Call $.smoothScroll if location.hash starts with "#smoothScroll"
+var reSmooth = /^#smoothScroll/;
+var id;
+if (reSmooth.test(location.hash)) {
+ // Strip the "#smoothScroll" part off (and put "#" back on the beginning)
+ id = '#' + location.hash.replace(reSmooth, '');
+ $.smoothScroll({scrollTarget: id});
+}
+```
+
+## Focus element after scrolling to it.
+
+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:
+
+```js
+$('a.example').smoothScroll({
+ afterScroll: function(options) {
+ $(options.scrollTarget).find('input')[0].focus();
+ }
+});
+
+```
+
+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:
+
+```js
+$('a.example').smoothScroll({
+ afterScroll: function(options) {
+ var $tgt = $(options.scrollTarget);
+ $tgt.attr('tabIndex', '0');
+ // Using $tgt[0] allows us to call .focus() on the DOM node itself, not the jQuery collection
+ $tgt[0].focus();
+ }
+});
+```
+
## Notes
* To determine where to scroll the page, the `$.fn.smoothScroll` method looks
From bf3b07cb53c15d5f903219f303368ded4546d695 Mon Sep 17 00:00:00 2001
From: Karl Swedberg Smooth Scroll jQuery Plugin with Back Button Support
+
diff --git a/readme.md b/readme.md
index 07c0976..3afb789 100644
--- a/readme.md
+++ b/readme.md
@@ -30,10 +30,9 @@ You can try a bare-bones demo at [kswedberg.github.io/jquery-smooth-scroll/demo/
* Exclude links if they are within a containing element: `$('#container a').smoothScroll({excludeWithin: ['.container2']});`
* Exclude links if they match certain conditions: `$('a').smoothScroll({exclude: ['.rough','#chunky']});`
* Adjust where the scrolling stops: `$('.backtotop').smoothScroll({offset: -100});`
-* Add a callback function that is triggered before the scroll starts: `$('a').smoothScroll({beforeScroll: function() { alert('ready to go!'); }});
+* Add a callback function that is triggered before the scroll starts: `$('a').smoothScroll({beforeScroll: function() { alert('ready to go!'); }});`
* Add a callback function that is triggered after the scroll is complete: `$('a').smoothScroll({afterScroll: function() { alert('we made it!'); }});`
-* Add back button support by including a history management plugin such as [Ben Alman's BBQ](http://benalman.com/code/projects/jquery-bbq/docs/files/jquery-ba-bbq-js.html). See [demo/bbq.html](demo/bbq.html) for an example of how to implement this.
-
+* Add back button support by using a [`hashchange` event listener](https://developer.mozilla.org/en-US/docs/Web/API/HashChangeEvent). You can also include a history management plugin such as [Ben Alman's BBQ](http://benalman.com/code/projects/jquery-bbq/docs/files/jquery-ba-bbq-js.html) for ancient browser support (IE < 8), but you'll need jQuery 1.8 or earlier. See [demo/hashchange.html](demo/hashchange.html) or [demo/bbq.html](demo/bbq.html) for an example of how to implement.
#### Options
@@ -49,7 +48,7 @@ The following options, shown with their default values, are available for both `
// only use if you want to override default behavior
scrollTarget: null,
- // string to use as selector for event delegation (Requires jQuery >=1.4.2)
+ // string to use as selector for event delegation
delegateSelector: null,
// fn(opts) function to be called before scrolling occurs.
@@ -59,6 +58,9 @@ The following options, shown with their default values, are available for both `
// fn(opts) function to be called after scrolling occurs.
// `this` is the triggering element
afterScroll: function() {},
+
+ // easing name. jQuery comes with "swing" and "linear." For others, you'll need an easing plugin
+ // from jQuery UI or elsewhere
easing: 'swing',
// speed can be a number or 'auto'
diff --git a/src/jquery.smooth-scroll.js b/src/jquery.smooth-scroll.js
index ff41dbb..41987d3 100644
--- a/src/jquery.smooth-scroll.js
+++ b/src/jquery.smooth-scroll.js
@@ -27,7 +27,14 @@
// fn(opts) function to be called after scrolling occurs.
// `this` is the triggering element
afterScroll: function() {},
+
+ // easing name. jQuery comes with "swing" and "linear." For others, you'll need an easing plugin
+ // from jQuery UI or elsewhere
easing: 'swing',
+
+ // speed can be a number or 'auto'
+ // if 'auto', the speed will be calculated based on the formula:
+ // (current scroll position - target scroll position) / autoCoeffic
speed: 400,
// coefficient for "auto" speed
From 6d865c0e3da84f85d3594249d58efc709fb7a4dd Mon Sep 17 00:00:00 2001
From: Karl Swedberg Scroll the Document to one of the following paragraphs: Try it with jQuery BBQ hashchange support Or, native hashchange support (IE9+) Toggle scrolling speed for the p1 link Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ YOU FOUND ME! Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+ p1 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. p2 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. p3 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. p4 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. p5 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. p6 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+
+