Coder Social home page Coder Social logo

scroll-behavior-polyfill's Introduction

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.