• Stars
    star
    102
  • Rank 324,261 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A polyfill for the 'scroll-behavior' CSS-property
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 CSS-property as well as the extensions to the Element interface in the CSSOM View Module 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 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

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:

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:

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

Alternatively, you can use Polyfill.app 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).

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:

<!-- Works just fine when given in the 'style' attribute -->
<div style="scroll-behavior: smooth"></div>
<!-- Works just fine when given as an attribute of the name 'scroll-behavior' -->
<div scroll-behavior="smooth"></div>

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

See this section 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:

// 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:

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 which takes care of this stuff automatically.

Contributing

Do you want to contribute? Awesome! Please follow these recommendations.

Maintainers

Frederik Wessberg
Frederik Wessberg
Twitter: @FredWessberg
Lead Developer

Backers

Patreon

Become a backer and get your name, avatar, and Twitter handle listed here.

Backers on Patreon

FAQ

Are there any known quirks?

License

MIT © Frederik Wessberg (@FredWessberg) (Website)

More Repositories

1

rollup-plugin-ts

A TypeScript Rollup plugin that bundles declarations, respects Browserslists, and enables seamless integration with transpilers such as babel and swc
TypeScript
480
star
2

cjstoesm

A tool that can transform CommonJS to ESM
TypeScript
362
star
3

DI

A compile-time powered Dependency-Injection container for Typescript that holds services and can produce instances of them as required.
TypeScript
241
star
4

Polyfiller

Never worry about polyfills again.
JavaScript
123
star
5

browserslist-generator

A library that makes generating and validating Browserslists a breeze!
TypeScript
82
star
6

ts-evaluator

An interpreter for Typescript that can evaluate an arbitrary Node within a Typescript AST
TypeScript
81
star
7

DI-compiler

A Custom Transformer for Typescript that enables compile-time Dependency Injection
TypeScript
74
star
8

ts-clone-node

A library that helps you clone Nodes from a Typescript AST
TypeScript
32
star
9

pointer-events

A Level 2 spec-compliant Pointer Events polyfill with first-class Shadow DOM support
TypeScript
16
star
10

sandhog

A virtual Open Source project maintainer
TypeScript
15
star
11

color

A library of well-tested helper methods for working with colors.
TypeScript
13
star
12

intl-relative-time-format

A fully spec-compliant polyfill for 'Intl.RelativeTimeFormat'
TypeScript
12
star
13

intl-list-format

A fully spec-compliant polyfill for 'Intl.ListFormat'
TypeScript
11
star
14

connection-observer

An API that provides a way to asynchronously observe the connectedness of a target Node inside a document
TypeScript
11
star
15

CodeAnalyzer

A service that parses and reflects on the AST generated by Typescript's language service. With it, we can extract metadata such as initialization values and types, arguments and import declarations.
TypeScript
6
star
16

marshaller

A lightweight way to serialize and deserialize complex data types non-destructively
TypeScript
5
star
17

sass-extended-importer

A Custom Sass Import Resolver with included support for Node Module Resolution and additional file extensions
TypeScript
4
star
18

compatfactory

A library that unifies the TypeScript Compiler API factory functions across all versions of TypeScript and makes them conform with the Node Factory API
TypeScript
4
star
19

crosspath

A wrapper around the path module that always normalizes to POSIX (including converting backslashes to forward slashes)
TypeScript
2
star
20

node.parentelement

A spec-compliant cross-browser polyfill for Node.parentElement
JavaScript
2
star
21

notes

ITU notes
2
star
22

helpertypes

A collection of TypeScript helper types
TypeScript
2
star
23

EmojiChat

EmojiChat is a based on a simple concept: It tracks your and head position and facial expressions in real-time and puts an Emoji on top of your head that represents your current mood.
TypeScript
2
star
24

symboltable

A Generic Symbol Table implementation for JavaScript based on a Red-Black Binary Search Tree. Typed with Flow and exported as ES-modules.
JavaScript
2
star
25

es7-string-polyfills

Polyfills for String.prototype.padStart and String.prototype.padEnd
JavaScript
1
star
26

iterators-polyfill

Brings Iterators and Iterables to JavaScript for older browsers.
JavaScript
1
star
27

es6-string-polyfills

Polyfills for all new es6 string methods (except String.normalize).
JavaScript
1
star
28

TypescriptASTUtil

A helper class for working with Typescript's AST
TypeScript
1
star
29

TypescriptLanguageService

A host-implementation of Typescripts LanguageService.
TypeScript
1
star