All Projects → wessberg → Scroll Behavior Polyfill

wessberg / Scroll Behavior Polyfill

Licence: mit
A polyfill for the 'scroll-behavior' CSS-property

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Scroll Behavior Polyfill

Scroll Into View If Needed
Element.scrollIntoView ponyfills for things like "if-needed" and "smooth"
Stars: ✭ 811 (+967.11%)
Mutual labels:  polyfill, smooth-scrolling, smooth
Moveto
A lightweight scroll animation javascript library without any dependency
Stars: ✭ 2,746 (+3513.16%)
Mutual labels:  smooth-scrolling, smooth
Scrollissimo
Javascript plugin for smooth scroll-controlled animations
Stars: ✭ 160 (+110.53%)
Mutual labels:  smooth-scrolling, smooth
Ngx Scrollbar
Custom overlay-scrollbars with native scrolling mechanism
Stars: ✭ 355 (+367.11%)
Mutual labels:  smooth-scrolling, smooth
smoovy
A collection of small and useful js packages (smooth scrolling, utils, etc.) preventing copy & paste
Stars: ✭ 25 (-67.11%)
Mutual labels:  smooth-scrolling, smooth
Mos
一个用于在 macOS 上平滑你的鼠标滚动效果或单独设置滚动方向的小工具, 让你的滚轮爽如触控板 | A lightweight tool used to smooth scrolling and set scroll direction independently for your mouse on macOS
Stars: ✭ 7,772 (+10126.32%)
Mutual labels:  smooth-scrolling, smooth
Fetch Jsonp
Make JSONP request like window.fetch
Stars: ✭ 932 (+1126.32%)
Mutual labels:  polyfill
Bootstrap Ie11
Internet Explorer 11 compatibility solution for Bootstrap 5
Stars: ✭ 47 (-38.16%)
Mutual labels:  polyfill
Dom4
Modern DOM functionalities for every browser
Stars: ✭ 903 (+1088.16%)
Mutual labels:  polyfill
Is countable Polyfill
A trivial but working polyfill for PHP 7.3 is_countable function. Supports PHP versions >= 5.3
Stars: ✭ 16 (-78.95%)
Mutual labels:  polyfill
Css Vars Ponyfill
Client-side support for CSS custom properties (aka "CSS variables") in legacy and modern browsers
Stars: ✭ 1,166 (+1434.21%)
Mutual labels:  polyfill
Document Register Element
A stand-alone working lightweight version of the W3C Custom Elements specification
Stars: ✭ 1,123 (+1377.63%)
Mutual labels:  polyfill
Es5 Polyfill
ECMAScript 5 Polyfill for IE-8
Stars: ✭ 31 (-59.21%)
Mutual labels:  polyfill
Polyfill Service
Javascript polyfills as a service. Java implementation.
Stars: ✭ 8 (-89.47%)
Mutual labels:  polyfill
Random compat
PHP 5.x support for random_bytes() and random_int()
Stars: ✭ 7,950 (+10360.53%)
Mutual labels:  polyfill
Aiflatswitch
Nicely animated flat design switch alternative to UISwitch
Stars: ✭ 904 (+1089.47%)
Mutual labels:  smooth
Mgs Camera
Unity plugin for control camera in scene.
Stars: ✭ 66 (-13.16%)
Mutual labels:  smooth
Polyfill Intl Normalizer
This component provides a fallback implementation for the Normalizer class provided by the Intl extension.
Stars: ✭ 896 (+1078.95%)
Mutual labels:  polyfill
Proxy Polyfill
Proxy object polyfill
Stars: ✭ 951 (+1151.32%)
Mutual labels:  polyfill
Patterns Library
AXA CH UI components library. Please share, comment, create issues and work with us!
Stars: ✭ 63 (-17.11%)
Mutual labels:  polyfill
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)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].