sweet-scroll.js
ECMAScript2015 Friendly, dependency-free smooth scroll library.
See Demo
FEATURES
- Dependecy-free!!
- ECMAScript2015 friendly
- Use
requestAnimationFrame
API (IE9 works in setTimeout
instead) - Supports vertical and horizontal scroll
- Supports dynamic trigger (event delegation)
- Supports container for the scroll
- Supports many easing types
USAGE
1. Install
via NPM
$ npm install sweet-scroll
use
import SweetScroll from "sweet-scroll"
via MANUAL
- Download the sweet-scroll.min.js
- Load it in the script tag.
<script src="sweet-scroll.min.js"></script>
2. Setup of HTML
<a href="#intro" data-scroll>Go to Introduction</a>
...
<div id="intro">Introduction</div>
3. Initialize SweetScroll
const sweetScroll = new SweetScroll({});
OPTIONS
The following options are applied by default. It can be customized as needed.
{
trigger: "[data-scroll]",
header: "[data-scroll-header]",
duration: 1000,
delay: 0,
easing: "easeOutQuint",
offset: 0,
verticalScroll: true,
horizontalScroll: false,
stopScroll: true,
beforeScroll: null,
afterScroll: null,
cancelScroll: null
}
Easings
Supports the following easing.
- Normal
- Quad
easeInQuad
easeOutQuad
easeInOutQuad
- Cubic
easeInCubic
easeOutCubic
easeInOutCubic
- Quart
easeInQuart
easeOutQuart
easeInOutQuart
- Quint
easeInQuint
easeOutQuint
(default)easeInOutQuint
- Sine
easeInSine
easeOutSine
easeInOutSine
- Expo
easeInExpo
easeOutExpo
easeInOutExpo
- Circ
easeInCirc
easeOutCirc
easeInOutCirc
- Elastic
easeInElastic
easeOutElastic
easeInOutElastic
- Back
easeInBack
easeOutBack
easeInOutBack
- Bounce
easeInBounce
easeOutBounce
easeInOutBounce
Live demo
Customizing Tips
Specifies the container
In the following example we have specified in the container for scrolling the #container
.
<div id="container">
<a href="#heading2" data-scroll>Go to Heading2</a>
...
<h2 id="heading2">Heading2</h2>
</div>
const sweetScroll = new SweetScroll({}, "#container");
const sweetScroll = new SweetScroll({}, document.getElementById("container"));
Add the data-scroll-header
attribute in order to offset the height of the fixed header.
<header data-scroll-header></header>
Specify the CSS Selector in header
option instead of the data-scroll-header
attribute.
const sweetScroll = new SweetScroll({
header: "#header"
});
Override of options for each element
You can override the default options by passing the option in JSON
format to the data-scroll-options
.
<a href="#target" data-scroll data-scroll-options='{"easing": "easeOutBounce"}'>Go to Target</a>
if you want to use in non anchor element
Will use the data-scroll attribute instead of href.
<button type="button" data-scroll="+=500">Scroll under 500px</button>
API
new SweetScroll(options = {}, container = "body, html")
to(distance, options = {})
toTop(distance, options = {})
toLeft(distance, options = {})
toElement($el, options = {})
update(options = {})
stop(gotoEnd = false)
destroy()
- Callbacks
beforeScroll: function(toScroll, trigger){}
cancelScroll: function(){}
after: function(toScroll, trigger){}
distance
to can specify the CSS Selector or scroll position.
Example:
const SweetScroll = new SweetScroll();
sweetScroll.to("#footer");
sweetScroll.to({top: 1000, left: 20});
sweetScroll.to([0, 1000]);
sweetScroll.to(500);
sweetScroll.to("top: 500, left: 100");
sweetScroll.to("+=500");
sweetScroll.to("-=200");
new SweetScroll(options = {}, container = "body, html")
options: {Object}
container: {String | HTMLElement}
Will generate a SweetScroll instance.
Example:
const sweetScroll = new SweetScroll({
duration: 1200,
easing: "easeOutBounce"
}, "#container");
to(distance, options = {})
distance: {String | Array | Object}
options: {Object}
Scroll animation to the specified distance
.
Example:
sweetScroll.to({top: 1500, left: 400});
toTop(distance, options = {})
distance: {String | Array | Object}
options: {Object}
Vertical scroll animation to the specified distance
.
Example:
sweetScroll.toTop(0);
toLeft(distance, options = {})
distance: {String | Array | Object}
options: {Object}
Horizontal scroll animation to the specified distance
.
Example:
sweetScroll.toLeft(1500);
toElement($el, options = {})
$el: {HTMLElement}
options: {Object}
Scroll animation to the specified HTMLElement
.
Example:
sweetScroll.toElement(document.getElementById("content"));
update(options = {})
options: {Object}
Will update the SweetScroll instance.
Primarily used in the case of option update.
Example:
sweetScroll.update({
trigger: "a[href^='#']"
duration: 3000
});
stop(gotoEnd = true)
gotoEnd: {boolean}
Will stop the current scroll animation.
Example:
sweetScroll.stop(true);
destroy()
Will destroy the SweetScroll instance.
Disable of the method and event handler.
Example:
sweetScroll.destory();
Callbacks
In beforeScroll
and afterScroll
, you will pass the coordinates and the triggering element in the argument.
In addition, you can stop the scrolling by return a beforeScroll
in false
.
Example:
const sweetScroll = new SweetScroll({
beforeScroll(toScroll, trigger) {
console.log("Before!!");
if (trigger && trigger.classList.contains("is-disabled")) {
return false;
}
},
cancelScroll() {
console.log("Cancel!!");
},
afterScroll(toScroll, trigger) {
console.log("After!!");
}
});
Extends Class:
The following is a pattern to override a method in the inheritance destination class.
class MyScroll extends SweetScroll {
beforeScroll(toScroll, trigger) {
if (trigger.classList.contains("is-disabled")) {
return false;
}
}
cancelScroll() {
console.log("Canell!!");
}
afterScroll(toScroll, trigger) {
console.log("After!!");
}
}
BROWSER SUPPORT
Works in IE9+
, and all modern browsers.
LICENCE
Released under the MIT Licence
AUTHOR
tsuyoshiwada
DEVELOPMENT
Initialization of the project.
$ cd /your/project/dir
$ git clone https://github.com/tsuyoshiwada/sweet-scroll.git
Install some dependencies.
$ npm install
Start the development.
You can access to the http://localhost:3000/
.
$ npm start
Run lint and testing.
$ npm test
Generates build file.
$ npm run build
Bugs, feature requests and comments are more than welcome in the issues