
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
sweet-scroll
Advanced tools
ECMAScript2015 Friendly, dependency-free smooth scroll library.
requestAnimationFrame
API (IE9 works in setTimeout
instead)$ npm install sweet-scroll
import SweetScroll from "sweet-scroll"
<script src="sweet-scroll.min.js"></script>
<a href="#intro" data-scroll>Go to Introduction</a>
...
<div id="intro">Introduction</div>
const sweetScroll = new SweetScroll({/* some options */});
The following options are applied by default. It can be customized as needed.
{
trigger: "[data-scroll]", // Selector for trigger (must be a valid css selector)
header: "[data-scroll-header]", // Selector for fixed header (must be a valid css selector)
duration: 1000, // Specifies animation duration in integer
delay: 0, // Specifies timer for delaying the execution of the scroll in milliseconds.
easing: "easeOutQuint", // Specifies the pattern of easing
offset: 0, // Specifies the value to offset the scroll position in pixels
verticalScroll: true, // Enable the vertical scroll
horizontalScroll: false, // Enable the horizontal scroll
stopScroll: true, // When fired wheel or touchstart events to stop scrolling
// Callbacks
beforeScroll: null,
afterScroll: null,
cancelScroll: null
}
Supports the following easing.
linear
easeInQuad
easeOutQuad
easeInOutQuad
easeInCubic
easeOutCubic
easeInOutCubic
easeInQuart
easeOutQuart
easeInOutQuart
easeInQuint
easeOutQuint
(default)easeInOutQuint
easeInSine
easeOutSine
easeInOutSine
easeInExpo
easeOutExpo
easeInOutExpo
easeInCirc
easeOutCirc
easeInOutCirc
easeInElastic
easeOutElastic
easeInOutElastic
easeInBack
easeOutBack
easeInOutBack
easeInBounce
easeOutBounce
easeInOutBounce
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>
// Specified in the CSS Selector
const sweetScroll = new SweetScroll({/* some options */}, "#container");
// Specified in the HTMLElement
const sweetScroll = new SweetScroll({/* some options */}, 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"
});
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>
Will use the data-scroll attribute instead of href.
<button type="button" data-scroll="+=500">Scroll under 500px</button>
new SweetScroll(options = {}, container = "body, html")
to(distance, options = {})
toTop(distance, options = {})
toLeft(distance, options = {})
toElement($el, options = {})
update(options = {})
stop(gotoEnd = false)
destroy()
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();
// CSS Selector of target element
sweetScroll.to("#footer");
// Object
sweetScroll.to({top: 1000, left: 20});
// Array (top:0, left:1000)
sweetScroll.to([0, 1000]);
// Number (Priority to vertical scroll position. by default.)
sweetScroll.to(500);
// String (Like object syntax)
sweetScroll.to("top: 500, left: 100");
// String (Relative position)
sweetScroll.to("+=500");
sweetScroll.to("-=200");
options: {Object}
container: {String | HTMLElement}
Will generate a SweetScroll instance.
Example:
const sweetScroll = new SweetScroll({
duration: 1200,
easing: "easeOutBounce"
}, "#container");
distance: {String | Array | Object}
options: {Object}
Scroll animation to the specified distance
.
Example:
sweetScroll.to({top: 1500, left: 400});
distance: {String | Array | Object}
options: {Object}
Vertical scroll animation to the specified distance
.
Example:
sweetScroll.toTop(0);
distance: {String | Array | Object}
options: {Object}
Horizontal scroll animation to the specified distance
.
Example:
sweetScroll.toLeft(1500);
$el: {HTMLElement}
options: {Object}
Scroll animation to the specified HTMLElement
.
Example:
sweetScroll.toElement(document.getElementById("content"));
options: {Object}
Will update the SweetScroll instance.
Primarily used in the case of option update.
Example:
sweetScroll.update({
trigger: "a[href^='#']"
duration: 3000
});
gotoEnd: {boolean}
Will stop the current scroll animation.
Example:
sweetScroll.stop(true);
Will destroy the SweetScroll instance.
Disable of the method and event handler.
Example:
sweetScroll.destory();
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({
// Stop scrolling case of trigger element that contains the `is-disabled` class.
beforeScroll(toScroll, trigger) {
console.log("Before!!");
if (trigger && trigger.classList.contains("is-disabled")) {
return false;
}
},
// If the `wheel` or `touchstart` event is called
cancelScroll() {
console.log("Cancel!!");
},
// Scroll animation is complete
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) {
// Stop scrolling case of trigger element that contains the `is-disabled` class.
if (trigger.classList.contains("is-disabled")) {
return false;
}
}
cancelScroll() {
console.log("Canell!!");
}
afterScroll(toScroll, trigger) {
console.log("After!!");
}
}
Works in IE9+
, and all modern browsers.
Released under the MIT Licence
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
0.2.4
FAQs
Modern and the sweet smooth scroll library.
The npm package sweet-scroll receives a total of 6,647 weekly downloads. As such, sweet-scroll popularity was classified as popular.
We found that sweet-scroll demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.