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.

6 Answers 6

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Stas, for me this is works: import { polyfill } from 'smoothscroll-polyfill'; polyfill();
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.
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' });
  }

1 Comment

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.
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' });

1 Comment

This used to work for me too but recently broke. Now I'm having to do it in the component like @caballerog suggests.
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.

1 Comment

stackoverflow.com/a/46534123/1438576 is the preferred angular solution (via polyfills.ts)
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/[email protected]/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' });


3 Comments

It is considered better practice to put this in scripts in .angular-cli.json, loading it from node_modules.
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 stackoverflow.com/a/42982811/2755616 but it doesnt seem to work.
@user663031 it is no longer considered best practice in angular for polyfills. See stackoverflow.com/a/46534123/1438576
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();
        });
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.