:lollipop: ECMAScript2015+ & TypeScript 友好,无依赖的平滑滚动库。
ECMAScript2015+ & TypeScript Friendly, dependency-free smooth scroll library.
:lollipop: See Demo
requestAnimationFrame APISee the Migration Guide
$ npm install sweet-scrollimport SweetScroll from 'sweet-scroll';Go to Introduction
...
You need to initialize an instance after DOMContentLoaded.
document.addEventListener(
'DOMContentLoaded',
() => {
const scroller = new SweetScroll({
/* some options */
});
},
false,
);The following options are applied by default. It can be customized as needed.
…Supports the following easing.
lineareaseInQuadeaseOutQuadeaseInOutQuadeaseInCubiceaseOutCubiceaseInOutCubiceaseInQuarteaseOutQuarteaseInOutQuarteaseInQuinteaseOutQuint (default)easeInOutQuinteaseInSineeaseOutSineeaseInOutSineeaseInExpoeaseOutExpoeaseInOutExpoeaseInCirceaseOutCirceaseInOutCircEasing functions that are not built in can pass functions directly.
const scroller = new SweetScroll({
easing: advancedEasingFunction,
});easeInElastic
const easeInElastic = (_, t, b, c, d) => {
let s = 1.70158;
let p = 0;
let a = c;
if (t === 0) return b;
if ((t /= d) === 1) return b + c;
if (!p) p = d * 0.3;
if (a
easeOutElastic
```typescript
const easeOutElastic = (_, t, b, c, d) => {
let s = 1.70158;
let p = 0;
let a = c;
if (t === 0) return b;
if ((t /= d) === 1) return b + c;
if (!p) p = d * 0.3;
if (a
easeInOutElastic
```typescript
const easeInOutElastic = (_, t, b, c, d) => {
let s = 1.70158;
let p = 0;
let a = c;
if (t === 0) return b;
if ((t /= d / 2) === 2) return b + c;
if (!p) p = d * (0.3 * 1.5);
if (a
#### Back
easeInBack
```typescript
const easeInBack = (_, t, b, c, d, s = 1.70158) => c * (t /= d) * t * ((s + 1) * t - s) + b;easeOutBack
const easeOutBack = (_, t, b, c, d, s = 1.70158) =>
c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;easeInOutBack
const easeInOutBack = (_, t, b, c, d, s = 1.70158) =>
(t /= d / 2)
#### Bounce
easeOutBounce
```typescript
const easeOutBounce = (_, t, b, c, d) => {
if ((t /= d)
easeInBounce
```typescript
const easeOutBounce = (_, t, b, c, d) => {
if ((t /= d) c - easeOutBounce(x, d - t, 0, c, d) + b;easeInOutBounce
const easeOutBounce = (_, t, b, c, d) => {
if ((t /= d) c - easeOutBounce(x, d - t, 0, c, d) + b;
const easeInOutBounce = (x, t, b, c, d) =>
t
---
[Live demo](http://tsuyoshiwada.github.io/sweet-scroll/easings.html)
## Customizing Tips
### Specifying container elements
In the following example we have specified in the container for scrolling the `#container`.
```html
// Specified in the CSS Selector
const scroller = new SweetScroll(
{
/* some options */
},
'#container',
);
// Specified in the Element
const scroller = new SweetScroll(
{
/* some options */
},
document.getElementById('container'),
);Add the data-scroll-header attribute in order to offset the height of the fixed header.
Specify the CSS Selector in header option instead of the data-scroll-header attribute.
const scroller = new SweetScroll({
header: '#header',
});You can override the default options by passing the option in JSON format to the data-scroll-options.
Go to TargetWill use the data-scroll attribute instead of href.
Scroll under 500pxThe following, Introduce one of the mounting method.
document.addEventListener(
'DOMContentLoaded',
() => {
const scroller = new SweetScroll();
const hash = window.location.hash;
const needsInitialScroll = document.getElementById(hash.substr(1)) != null;
if (needsInitialScroll) {
window.location.hash = '';
}
window.addEventListener(
'load',
() => {
if (needsInitialScroll) {
scroller.to(hash, { updateURL: 'replace' });
}
},
false,
);
},
false,
);You can also achieve the same thing in other ways by using the provided API.
Will generate a SweetScroll instance.
Example:
const scroller = new SweetScroll(
{
duration: 1200,
easing: 'easeOutExpo',
},
'#container',
);Will generate a SweetScroll instance. (factory method)
Example:
const scroller = SweetScroll.create(
{
duration: 1200,
easing: 'easeOutExpo',
},
'#container',
);Scroll animation to the specified distance.
distance to can specify the CSS Selector or scroll position.
Example:
const scroller = new SweetScroll();
// CSS Selector of target element
scroller.to('#footer');
// Object
scroller.to({ top: 1000, left: 20 });
// Array (top:0, left:1000)
scroller.to([0, 1000]);
// Number (Priority to vertical scroll position. by default.)
scroller.to(500);
// String (Relative position)
scroller.to('+=500');
scroller.to('-=200');Vertical scroll animation to the specified distance.
Example:
scroller.toTop(0);Horizontal scroll animation to the specified distance.
Example:
scroller.toLeft(1500);Scroll animation to the specified Element.
Example:
scroller.toElement(document.getElementById('content'));Will update the SweetScroll instance. Primarily used in the case of option update.
Example:
scroller.update({
trigger: 'a[href^="#"]',
duration: 3000,
});gotoEnd: {Boolean}
Will stop the current scroll animation.
Example:
scroller.stop(true);Will destroy the SweetScroll instance. Disable of the method and event handler.
Example:
scroller.destroy();In before and after, you will pass the coordinates and the triggering element in the argument.
In addition, you can stop the scrolling by return a before in false.
Example:
…Extends Class:
The following is a pattern to override a method in the inheritance destination class.
…Works in IE10+, and all modern browsers.
It is necessary to use polyfill or ponyfill of requestAnimationFrame.
Example ponyfill
Using raf module.
import raf from 'raf';
import SweetScroll from 'sweet-scroll';
SweetScroll.raf = raf;
SweetScroll.caf = raf.cancel;See the CHANGELOG.md
git checkout -b my-new-featuregit commit -am 'Add some feature'git push origin my-new-featureBugs, feature requests and comments are more than welcome in the issues.
We will develop using the following npm scripts.
yarn startLaunch the local server and let the demo run. Opening http://localhost:3000 in your browser.
yarn buildCompile TypeScript and create type definitions.
yarn testRun unit testing with Jest.
暂无开放 Issues,或尚未同步最近议题。