百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

sweet-scroll

> 编程语言
开源

:lollipop: ECMAScript2015+ & TypeScript 友好,无依赖的平滑滚动库。

392 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

:lollipop: ECMAScript2015+ & TypeScript 友好,无依赖的平滑滚动库。

ECMAScript2015+ & TypeScript Friendly, dependency-free smooth scroll library.

:lollipop: See Demo

Features

  • Dependecy-free!!
  • ECMAScript2015+ & TypeScript friendly
  • Use requestAnimationFrame API
  • Supports vertical and horizontal scroll
  • Supports dynamic trigger (event delegation)
  • Supports container for the scroll
  • Supports many easing types
  • Supports server-side rendering (Can load without putting out errors.)

Migration Guide

See the Migration Guide

Table of Contents

  • Usage
    • 1. Install
    • 2. Setup of HTML
    • 3. Initialize SweetScroll
  • Options
  • Easings
    • Built-in (22)
    • Advanced (9)
  • Customizing Tips
    • Specifying container elements
    • Specify fixed header
    • Override of options for each element
    • If you want to use in non anchor element
    • Do you feel scrolling is slow?
    • Scrolling animation in another page
  • API
    • new SweetScroll(options?: PartialOptions, container?: string | Element)
    • SweetScroll.create(options?: PartialOptions, container?: string | Element)
    • to(distance: any, options?: PartialOptions)
    • toTop(distance: any, options?: PartialOptions)
    • toLeft(distance: any, options?: PartialOptions)
    • toElement($el: Element, options?: PartialOptions)
    • update(options: PartialOptions)
    • stop(gotoEnd: boolean = true)
    • destroy()
    • Callbacks
  • Browser Support
    • Scrolling with IE9
  • CHANGELOG
  • Contibute
    • Development
  • License

Usage

1. Install

via NPM

bash
$ npm install sweet-scroll
use
typescript
import SweetScroll from 'sweet-scroll';

via MANUAL

  1. Download the sweet-scroll.min.js
  2. Load it in the script tag.
xml

via CDN (UNPKG)

xml

2. Setup of HTML

xml
Go to Introduction
...

3. Initialize SweetScroll

You need to initialize an instance after DOMContentLoaded.

typescript
document.addEventListener(
  'DOMContentLoaded',
  () => {
    const scroller = new SweetScroll({
      /* some options */
    });
  },
  false,
);

Options

The following options are applied by default. It can be customized as needed.

…

Easings

Supports the following easing.

Built-in (22)

  • Normal
    • linear
  • 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

Advanced (9)

Easing functions that are not built in can pass functions directly.

typescript
const scroller = new SweetScroll({
  easing: advancedEasingFunction,
});

Elastic

easeInElastic

typescript
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

typescript
const easeOutBack = (_, t, b, c, d, s = 1.70158) =>
  c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;

easeInOutBack

typescript
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

typescript
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
typescript
// 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'),
);

Specify fixed header

Add the data-scroll-header attribute in order to offset the height of the fixed header.

xml

Specify the CSS Selector in header option instead of the data-scroll-header attribute.

typescript
const scroller = 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.

xml
Go to Target

If you want to use in non anchor element

Will use the data-scroll attribute instead of href.

xml
Scroll under 500px

Scrolling animation in another page

The following, Introduce one of the mounting method.

typescript
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,
);

Live demo

You can also achieve the same thing in other ways by using the provided API.

API

new SweetScroll(options?: PartialOptions, container?: string | Element | Window)

Will generate a SweetScroll instance.

Example:

typescript
const scroller = new SweetScroll(
  {
    duration: 1200,
    easing: 'easeOutExpo',
  },
  '#container',
);

SweetScroll.create(options?: PartialOptions, container?: string | Element | Window)

Will generate a SweetScroll instance. (factory method)

Example:

typescript
const scroller = SweetScroll.create(
  {
    duration: 1200,
    easing: 'easeOutExpo',
  },
  '#container',
);

to(distance: any, options?: PartialOptions)

Scroll animation to the specified distance. distance to can specify the CSS Selector or scroll position.

Example:

typescript
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');

toTop(distance: any, options?: PartialOptions)

Vertical scroll animation to the specified distance.

Example:

typescript
scroller.toTop(0);

toLeft(distance: any, options?: PartialOptions)

Horizontal scroll animation to the specified distance.

Example:

typescript
scroller.toLeft(1500);

toElement($el: Element, options?: PartialOptions)

Scroll animation to the specified Element.

Example:

typescript
scroller.toElement(document.getElementById('content'));

update(options: PartialOptions)

Will update the SweetScroll instance. Primarily used in the case of option update.

Example:

typescript
scroller.update({
  trigger: 'a[href^="#"]',
  duration: 3000,
});

stop(gotoEnd: boolean = true)

gotoEnd: {Boolean}

Will stop the current scroll animation.

Example:

typescript
scroller.stop(true);

destroy()

Will destroy the SweetScroll instance. Disable of the method and event handler.

Example:

typescript
scroller.destroy();

Callbacks

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.

…

Browser Support

Works in IE10+, and all modern browsers.

Scrolling with IE9

It is necessary to use polyfill or ponyfill of requestAnimationFrame.

Example ponyfill

Using raf module.

typescript
import raf from 'raf';
import SweetScroll from 'sweet-scroll';

SweetScroll.raf = raf;
SweetScroll.caf = raf.cancel;

CHANGELOG

See the CHANGELOG.md

Contibute

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :muscle:

Bugs, feature requests and comments are more than welcome in the issues.

Development

We will develop using the following npm scripts.

yarn start

Launch the local server and let the demo run. Opening http://localhost:3000 in your browser.

yarn build

Compile TypeScript and create type definitions.

yarn test

Run unit testing with Jest.

License

MIT © tsuyoshiwada

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptanimationes2015scrollingsmooth-scrolling

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言