项目作者: jonaskuske

项目描述 :
⚓ Apply smooth scroll to anchor links, polyfill scroll-behavior
高级语言: JavaScript
项目地址: git://github.com/jonaskuske/smoothscroll-anchor-polyfill.git
创建时间: 2018-11-10T21:01:29Z
项目社区:https://github.com/jonaskuske/smoothscroll-anchor-polyfill

开源协议:MIT License

下载



NPM version
Build status
License
Documentation

smoothscroll-anchor-polyfill

⚓ Apply smooth scroll to anchor links to polyfill the CSS property scroll-behavior

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.3KB gzipped

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

Browser support

IE / Edge
IE / Edge
Firefox
Firefox
Chrome
Chrome
Safari
Safari
iOS Safari
iOS Safari
Opera
Opera
IE9+, Edge native native* last 2 versions last 2 versions native*

* 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, just specyfing the normal scroll-behavior property is not enough unfortunately.
You need to add an additional CSS variable so the polyfill can read it:

  1. html {
  2. --scroll-behavior: smooth;
  3. scroll-behavior: smooth;
  4. }

You can also use media queries, toggle classes etc. to control the smooth scroll. The following only enables smooth scroll on Desktop devices, for example:

  1. html {
  2. --scroll-behavior: auto;
  3. scroll-behavior: auto;
  4. }
  5. @media screen and (min-width: 1150px) {
  6. html {
  7. --scroll-behavior: smooth;
  8. scroll-behavior: smooth;
  9. }
  10. }

💡 This process can be automated using a PostCSS plugin, so you can write regular CSS and it’ll be transformed to work with the polyfill automatically.
The plugin will also read your browserslist and choose the right transformation depending on if all your browsers support CSS variables or not. It just works™

Need to support Internet Explorer?

Legacy browsers like Internet Explorer do not support CSS variables, so you need another way to specify scroll-behavior. There are two options:

Using the inline style attribute
  1. <html style="scroll-behavior: smooth;">
  2. ...
  3. </html>
Using font-family

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). As with CSS variables (and unlike inline styles), this allows you to use normal CSS features like media queries.

  1. <style>
  2. html {
  3. scroll-behavior: auto;
  4. font-family: 'scroll-behavior: auto;', 'Roboto', sans-serif;
  5. }
  6. </style>

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

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

2b. From npm

  1. npm install smoothscroll-anchor-polyfill

then

  1. 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!

© 2021, Jonas Kuske