Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wessberg/scroll-behavior-polyfill

A polyfill for the 'scroll-behavior' CSS-property
https://github.com/wessberg/scroll-behavior-polyfill

css polyfill scroll-behavior smooth smooth-scrolling

Last synced: 18 days ago
JSON representation

A polyfill for the 'scroll-behavior' CSS-property

Awesome Lists containing this project

README

        

Logo

> A polyfill for the 'scroll-behavior' CSS-property

Downloads per month
NPM version
Dependencies
Contributors
code style: prettier
License: MIT
Support on Patreon

## Description

The [`scroll-behavior`](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior) CSS-property as well as the extensions to the Element interface in the [CSSOM View Module](https://drafts.csswg.org/cssom-view/#dom-element-scrollto-options-options) CSS property sets the behavior for a scrolling box when scrolling is triggered by the navigation or CSSOM scrolling APIs.
This polyfill brings this new feature to all browsers.

It is very efficient, tiny, and works with the latest browser technologies such as Shadow DOM.

This polyfill also implements the extensions to the Element interface in the [CSSOM View Module](https://drafts.csswg.org/cssom-view/#dom-element-scrollto-options-options) such as `Element.prototype.scroll`, `Element.prototype.scrollTo`, `Element.protype.scrollBy`, and `Element.prototype.scrollIntoView`.

### Features

- Spec-compliant
- Tiny
- Efficient
- Works with the latest browser technologies, including Shadow DOM
- Seamless

## Table of Contents

- [Description](#description)
- [Features](#features)
- [Table of Contents](#table-of-contents)
- [Install](#install)
- [NPM](#npm)
- [Yarn](#yarn)
- [Applying the polyfill](#applying-the-polyfill)
- [Usage](#usage)
- [Declarative API](#declarative-api)
- [Imperative API](#imperative-api)
- [Dependencies & Browser support](#dependencies--browser-support)
- [Contributing](#contributing)
- [Maintainers](#maintainers)
- [Backers](#backers)
- [Patreon](#patreon)
- [FAQ](#faq)
- [Are there any known quirks?](#are-there-any-known-quirks)
- [License](#license)

## Install

### NPM

```
$ npm install scroll-behavior-polyfill
```

### Yarn

```
$ yarn add scroll-behavior-polyfill
```

## Applying the polyfill

The polyfill will be feature detected and applied if and only if the browser doesn't support the property already.
To include it, add this somewhere:

```typescript
import "scroll-behavior-polyfill";
```

However, it is strongly suggested that you only include the polyfill for browsers that doesn't already support `scroll-behavior`.
One way to do so is with an async import:

```typescript
if (!("scrollBehavior" in document.documentElement.style)) {
await import("scroll-behavior-polyfill");
}
```

Alternatively, you can use [Polyfill.app](https://github.com/wessberg/Polyfiller) which uses this polyfill and takes care of only loading the polyfill if needed as well as adding the language features that the polyfill depends on (See [dependencies](#dependencies--browser-support)).

## Usage

### Declarative API

You can define the `scroll-behavior` of Elements via one of the following approaches:

- A style attribute including a `scroll-behavior` property.
- An element with a `scroll-behavior` attribute.
- Or, an element with a `CSSStyleDeclaration` with a `scrollBehavior` property.

This means that either of the following approaches will work:

```html

// Works jut fine when given as a style property
element.style.scrollBehavior = "smooth";

```

See [this section](#are-there-any-known-quirks) for information about why `scroll-behavior` values provided in stylesheets won't be discovered by the polyfill.

### Imperative API

You can of course also use the imperative `scroll()`, `scrollTo`, `scrollBy`, and `scrollIntoView` APIs and provide `scroll-behavior` options.

For example:

```typescript
// Works for the window object
window.scroll({
behavior: "smooth",
top: 100,
left: 0
});

// Works for any element (and supports all options)
myElement.scrollIntoView();

myElement.scrollBy({
behavior: "smooth",
top: 50,
left: 0
});
```

You can also use the `scrollTop` and `scrollLeft` setters, both of which works with the polyfill too:

```typescript
element.scrollTop += 100;
element.scrollLeft += 50;
```

## Dependencies & Browser support

This polyfill is distributed in ES3-compatible syntax, but is using some modern APIs and language features which must be available:

- `requestAnimationFrame`
- `Object.getOwnPropertyDescriptor`
- `Object.defineProperty`

For by far the most browsers, these features will already be natively available.
Generally, I would highly recommend using something like [Polyfill.app](https://github.com/wessberg/Polyfiller) which takes care of this stuff automatically.

## Contributing

Do you want to contribute? Awesome! Please follow [these recommendations](./CONTRIBUTING.md).

## Maintainers

| Frederik Wessberg |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Frederik Wessberg](mailto:[email protected])
Twitter: [@FredWessberg](https://twitter.com/FredWessberg)
_Lead Developer_ |

## Backers

### Patreon

[Become a backer](https://www.patreon.com/bePatron?u=11315442) and get your name, avatar, and Twitter handle listed here.

Backers on Patreon

## FAQ

### Are there any known quirks?

- `scroll-behavior` properties declared only in stylesheets won't be discovered. This is because [polyfilling CSS is hard and really bad for performance](https://philipwalton.com/articles/the-dark-side-of-polyfilling-css/).

## License

MIT © [Frederik Wessberg](mailto:[email protected]) ([@FredWessberg](https://twitter.com/FredWessberg)) ([Website](https://github.com/wessberg))