1.3.4 • Published 2 years ago

smoothscroll-anchor-polyfill v1.3.4

Weekly downloads
2,001
License
MIT
Repository
github
Last release
2 years ago

 
 

 
 
 

Features

  • ✔ Smooth scroll to target when clicking an anchor
  • ✔ Smooth scroll to target on hashchange (◀/▶ buttons)
  • ✔ Updates URL with #fragment
  • ✔ Handles focus for improved accessibility
  • ✔ Doesn't break server-side rendering
  • ✔ 1.2KB gzipped

⚠ Requires smooth scroll for window.scroll() and Element.scrollIntoView() (e.g. smoothscroll-polyfill) to work!

 

Browser support

IE / EdgeFirefoxChromeSafariiOS SafariOpera
IE9+, Edgenativenative*last 2 versionslast 2 versionsnative*

* hashchange navigation triggered by forwards/backwards buttons isn't smooth despite native support. Learn more

 

Usage

1. Set scroll-behavior: smooth in CSS

Has to be set global (on html), check the docs for limitations

Because CSS properties unknown to a browser can't efficiently be parsed from JavaScript, using normal stylesheets is not enough unfortunately. To specify the property in a way the polyfill can read it, you have two options:

1a. Using inline styles

<html style="scroll-behavior: smooth;">
...
</html>

1b. Using font-family as workaround

Alternatively, you can specify the property as the name of a custom font family. Your actual fonts will still work the way they should (plus, you can simply declare actual fonts on body). Unlike inline styles, this allows you to use normal CSS features like media queries. The following only enables smooth scroll on desktop devices, for example:

<style>
  html {
    scroll-behavior: auto;
    font-family: 'scroll-behavior: auto;', 'Roboto', sans-serif;
  }

  @media screen and (min-width: 1150px) {
    html {
      scroll-behavior: smooth;
      font-family: 'scroll-behavior: smooth;', 'Roboto', sans-serif;
    }
  }
</style>

This process can be automated using a PostCSS plugin, so you can write regular CSS and don't have to bother with font-families. It just works™

2. Install the polyfill

Because this polyfill only wires up anchor links to use the browser's native window.scroll() and element.scrollIntoView() methods, you'll need to load a polyfill providing smooth scroll to these methods in addition to the steps outlined below.

smoothscroll-polyfill works, but you can just as well use another one or write your own implementation. Learn More

2a. From a CDN

<script src="https://unpkg.com/smoothscroll-anchor-polyfill"></script>

2b. From npm

npm install smoothscroll-anchor-polyfill

then

import 'smoothscroll-anchor-polyfill';

 

Full Documentation & Demo

The full documentation with advanced installation instructions, limitations, features like enabling and disabling the smooth scrolling and more can be found at jonaskuske.github.io/smoothscroll-anchor-polyfill.
The documentation site itself is built as a smooth scrolling one-page design, utilizing this polyfill.

 
 


PRs welcome!

 

© 2019, Jonas Kuske

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

5 years ago

1.3.0-beta.1

5 years ago

1.3.0-beta.0

5 years ago

1.2.0

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.0-beta

5 years ago

0.12.0

5 years ago

0.9.4

5 years ago

0.9.3

5 years ago

0.9.2

5 years ago

0.9.1

5 years ago

0.9.0

5 years ago