3

Can anyone please guide how to use Smooth Scroll Polyfill with Angular 2 CLI.

I tried adding below to polyfills.ts but it throws error on work require

require('smoothscroll-polyfill').polyfill();

I then tried adding

import 'smoothscroll-polyfill';

Though it didn't throw any error during build but when I run the project in browser it show below error on console:

ERROR TypeError: Failed to execute 'scroll' on 'Window': No function was found that matched the signature provided.
Naveed Ahmed
  • 10,048
  • 12
  • 46
  • 85

6 Answers6

8

Here's the Angular 2/4 solution:

Install smoothscroll-polyfill - npm install smoothscroll-polyfill.

Then add to your src/polyfills.ts file:

import smoothscroll from 'smoothscroll-polyfill/dist/smoothscroll.js';
smoothscroll.polyfill();
Geoff James
  • 3,122
  • 1
  • 17
  • 36
Stas
  • 139
  • 1
  • 5
  • 1
    Thank you Stas, for me this is works: import { polyfill } from 'smoothscroll-polyfill'; polyfill(); – Naveed Ahmed Jun 14 '17 at 19:03
  • I prefer this method as it helps keep polyfills out of my main code and satisfy my separation of concerns. Could also install the `@types/smoothscroll-polyfill` to keep linters happy, as others have suggested, but this is not essential. – Geoff James May 10 '19 at 09:12
6

You need install the polyfill and @types

  1. yarn add smoothscroll-polyfill or npm install smoothscroll-polyfill
  2. yarn add @types/smoothscroll-polyfill or npm install @types/smoothscroll-polyfill

So, in your code you should init the polyfill in the component or service what you wish used this polyfill:

import * as smoothscroll from "smoothscroll-polyfill";

@Component({
  selector: 'app-my-demo',
  templateUrl: './app-my-demo.html',
  styleUrls: ['./app-my-demo.css']
})
export MyClass my implements OnInit {

  constructor(
  ) {
    smoothscroll.polyfill();
   }

You could use in the component, for example:

  clickAnyThing(event:any){
    window.scroll({ top: 0, left: 0, behavior: 'smooth' });
  }
caballerog
  • 2,679
  • 1
  • 19
  • 32
  • I much prefer @Stas' solution, as I would rather keep polyfills out of my main code. I believe the `polyfills.ts` file exists in Angular2+ projects set up from CLI. – Geoff James May 10 '19 at 09:24
6

This is how I set it up in my polyfills.ts file:

import smoothscroll from 'smoothscroll-polyfill/dist/smoothscroll';
smoothscroll.polyfill();

Then you can use it like this:

your_element.scrollIntoView({ behavior: 'smooth' });
Kevin LeStarge
  • 8,142
  • 4
  • 21
  • 34
  • 1
    This used to work for me too but recently broke. Now I'm having to do it in the component like @caballerog suggests. – Methodician Feb 16 '18 at 18:48
2

If you are using angular cli

First install the package:

npm install smoothscroll-polyfill

Then add the following to angular-cli.json under apps.scripts array:

"../node_modules/smoothscroll-polyfill/src/smoothscroll.js"

And then try something like:

window.scroll({ top: 400, left: 0, behavior: 'smooth' });

It should work.

Nylon Smile
  • 8,990
  • 1
  • 25
  • 34
1

#option 1

It can be included directly in the index.html it will run immediately and does not need to be invoked with smoothscroll.polyfill();

<script src="https://unpkg.com/smoothscroll-polyfill@0.4.3/dist/smoothscroll.min.js"></script>

#option 2

npm install smoothscroll-polyfill --save

including it in src/polyfills.ts

import 'smoothscroll-polyfill';

or adding it to scripts in .angular-cli.json should work.


Importing in the Component:

import  { polyfill } from "smoothscroll-polyfill"

polyfill()
  • #use:

window.scroll({ top: 200, left: 0, behavior: 'smooth' });


KB_
  • 2,113
  • 2
  • 26
  • 28
  • 3
    It is considered better practice to put this in `scripts` in `.angular-cli.json`, loading it from `node_modules`. –  Apr 03 '17 at 02:53
  • 1
    Thank you for your reply but since there is a file with name polyfills.ts in angular cli project I believe polyfills should be added in this file. I came across this http://stackoverflow.com/a/42982811/2755616 but it doesnt seem to work. – Naveed Ahmed Apr 03 '17 at 15:00
  • @user663031 it is no longer considered best practice in angular _for polyfills_. See https://stackoverflow.com/a/46534123/1438576 – runderworld Aug 12 '20 at 21:51
1

If you're using it with Typescript then you should install

npm install smoothscroll-polyfill @types/smoothscroll-polyfill

OR

yarn add smoothscroll-polyfill @types/smoothscroll-polyfill/

Then you can use it like below

    import * as smoothscroll from "smoothscroll-polyfill";
    smoothscroll.polyfill();


Alternatively, you can include polyfill only when it is needed. you can try something below if you're using webpack, parcel or any other build system which supports dynamic imports.

    if (!'scrollBehavior' in document.documentElement.style) {
    // Wait until the Polyfills are loaded
    Promise.all([
        import('smoothscroll-polyfill'),
        import('smoothscroll-anchor-polyfill')
    ])
        // then use the modules however you want
        .then(([smoothscrollPolyfill, smoothscrollAnchorPolyfill]) => {
        // (Unlike this package, smoothscroll-polyfill needs to be actively invoked: )
        smoothscroll.polyfill();
        });
    }

Ankit Balyan
  • 1,319
  • 19
  • 31