Skip to content

Commit fe35787

Browse files
author
Steve Piron
committed
v1.0.0
Signed-off-by: Steve Piron <steve@basedigital.io>
1 parent bf8e110 commit fe35787

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
1-
# scroll-to
1+
# jQuery scroll to
22
A jQuery plugin that animates page jumps to anchors.
3+
4+
## Default initialization
5+
```js
6+
$('.js-scroll-to').spScrollTo();
7+
```
8+
9+
## Settings
10+
Option | Type | Default | Description
11+
------ | ---- | ------- | -----------
12+
container | jQuery object | $('body') | The containing element to scroll.
13+
offset | integer | 0 | Distance to the top of the viewport.
14+
scrollSpeed | integer | 800 | The duration of the scroll animation.
15+
onBeforeScroll | function | undefined | A function to be executed after reaching the target.
16+
17+
## Initialization with custom settings
18+
```js
19+
/**
20+
* 1. Remove 5px more from the cropped (calculated) height.
21+
*/
22+
$('.js-scroll-to').spScrollTo({
23+
container: $('html'),
24+
offset: $('#js-site-header').outerHeight()
25+
});
26+
```
27+
28+
### Dependencies
29+
jQuery
30+
31+
### License
32+
Copyright © Steve Piron

sp.scroll-to.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**!
2+
* @author Steve Piron <https://twitter.com/stevepiron>
3+
* @requires jQuery
4+
*
5+
* A jQuery plugin that will animate page jumps to anchors.
6+
*/
7+
(function( $ ) {
8+
9+
var defaults;
10+
var params;
11+
12+
var $body = $('body');
13+
14+
// ====================================================================== //
15+
// Functions
16+
// ====================================================================== //
17+
18+
/**
19+
* @function `scrollToTarget`
20+
*
21+
* Scrolls through the page to the defined target.
22+
*
23+
* @param $link {jQuery} - the clicked link.
24+
*/
25+
function scrollToTarget( $link ) {
26+
var $target = $($link.attr('href'));
27+
28+
params.container.animate({
29+
scrollTop: $target.offset().top - params.offset
30+
}, params.scrollSpeed);
31+
}
32+
33+
// ====================================================================== //
34+
// Plugin
35+
// ====================================================================== //
36+
37+
$.fn.spScrollTo = function( options ) {
38+
39+
/**
40+
* Note: using `return` keeps jQuery's chaining possibility
41+
*/
42+
return this.each(function() {
43+
44+
defaults = {
45+
container: $body,
46+
offset: 0,
47+
scrollSpeed: 800,
48+
onBeforeScroll: undefined
49+
};
50+
51+
params = $.extend( defaults, options );
52+
53+
var $this = $(this);
54+
55+
// ============================================================== //
56+
// On click
57+
// ============================================================== //
58+
59+
$this.on('click', function(e) {
60+
e.preventDefault();
61+
if( typeof params.onBeforeScroll == 'function' ) {
62+
params.onBeforeScroll(e);
63+
}
64+
scrollToTarget( $this );
65+
return;
66+
});
67+
68+
});
69+
};
70+
71+
}( jQuery ));

0 commit comments

Comments
 (0)