Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@hkjeffchan/ngx-scroll-to

nicky-lenaers1.6kMIT18.0.0TypeScript support: included

A simple Angular 4+ plugin enabling you to smooth scroll to any element on your page and enhance scroll-based features in your app.

ng, ngx, ngx-scroll-to, scroll, scrolling, scroll-to, smooth scroll, smooth-scroll, smooth scrolling, smooth-scrolling, smooth, angular, angular2, angular 2, angular4, angular 4, angular5, angular 5, angular6, angular 6, angular7, angular 7, angular8, angular 8, angular9, angular 9, angular10, angular 10, angular11, angular 11, angular12, angular 12, angular13, angular 13, angular14, angular 14

readme

THIS IS A FORK FOR @nicky-lenaers/ngx-scroll-to (I release for most updated angular version only)

ngx-scroll-to

A simple Angular 4+ plugin enabling you to smooth scroll to any element on your page and enhance scroll-based features in your app. Works for Angular 4+, both AoT and SSR. No dependencies.

Support for Angular 15!

Subject Type Badge
CI / CD Circle CI Circle CI
Releases GitHub GitHub Release
  NPM NPM Release
Dependencies Production Production Dependencies
  Peer Peer Dependencies
  Development Development Dependencies
  Optional Optional Dependencies
Downloads NPM NPM Monthly Downloads
License MIT License

Current Angular Version

npm version

Installation

Angular 15

$ npm install @nicky-lenaers/ngx-scroll-to

Angular 14

$ npm install @nicky-lenaers/ngx-scroll-to@'14'

Angular 13

$ npm install @nicky-lenaers/ngx-scroll-to@'13'

Angular 8 and 9

$ npm install @nicky-lenaers/ngx-scroll-to@'9'

Angular 7

$ npm install @nicky-lenaers/ngx-scroll-to@'2'

Angular 6

$ npm install @nicky-lenaers/ngx-scroll-to@'1'

Angular <= 5.x

$ npm install @nicky-lenaers/ngx-scroll-to@'<1'

Setup

...
import { ScrollToModule } from '@nicky-lenaers/ngx-scroll-to';
...

@NgModule({
  imports: [
    ...
    ScrollToModule.forRoot()
  ],
  ...
  bootstrap: [AppComponent]
})
export class AppModule { }

Basic Usage - Directive to Target

my.component.html

<!-- Works for including '#' -->
<button [ngxScrollTo]="'#destination'">Go to destination</button>

<!-- Works for excluding '#' -->
<button [ngxScrollTo]="'destination'">Go to destination</button>

<!-- Works for Angular ElementRef -->
<button [ngxScrollTo]="destinationRef">Go to destination</button>

<div id="destination" #destinationRef>You've reached your destination.</div>

Basic Usage - Directive to Offset Only

Besides scrolling to a specific element, it is also possible to scroll a given offset only. This can be achieved by an empty target and an offset:

my.component.html

<button ngxScrollTo [ngxScrollToOffset]="200">Go down 200 pixels</button>

Basic Usage - Service to Target

my.component.html

<button (click)="triggerScrollTo()">Go to destination</button>

<div id="destination">You've reached your destination.</div>

my.service.ts

import { Injectable } from "@angular/core";
import {
  ScrollToService,
  ScrollToConfigOptions,
} from "@nicky-lenaers/ngx-scroll-to";

@Injectable()
export class MyService {
  constructor(private scrollToService: ScrollToService) {}

  triggerScrollTo() {
    const config: ScrollToConfigOptions = {
      target: "destination",
    };

    this.scrollToService.scrollTo(config);
  }
}

Basic Usage - Service to Offset Only

Just like with the Directive, the Service can be used to scroll to an offset only instead of a given target element:

my.component.html

<button (click)="triggerScrollToOffsetOnly(200)">Go down 200 pixels</button>

my.service.ts

import { Injectable } from "@angular/core";
import {
  ScrollToService,
  ScrollToConfigOptions,
} from "@nicky-lenaers/ngx-scroll-to";

@Injectable()
export class MyService {
  constructor(private scrollToService: ScrollToService) {}

  triggerScrollToOffsetOnly(offset: number = 0) {
    const config: ScrollToConfigOptions = {
      offset,
    };

    this.scrollToService.scrollTo(config);
  }
}

Advanced Usage - Directive

my.component.ts

import {
  ScrollToAnimationEasing,
  ScrollToEvent,
  ScrollToOffsetMap,
} from "@nicky-lenaers/ngx-scroll-to";

@Component({
  selector: "my-component",
  templateUrl: "./my.component.html",
})
export class MyComponent {
  ngxScrollToDestination: string;
  ngxScrollToEvent: ScrollToEvent;
  ngxScrollToDuration: number;
  ngxScrollToEasing: ScrollToAnimationEasing;
  ngxScrollToOffset: number;
  ngxScrollToOffsetMap: ScrollToOffsetMap;

  constructor() {
    this.ngxScrollToDestination = "destination-1";
    this.ngxScrollToEvent = "mouseenter";
    this.ngxScrollToDuration = 1500;
    this.ngxScrollToEasing = "easeOutElastic";
    this.ngxScrollToOffset = 300;
    this.ngxScrollToOffsetMap = new Map();
    this.ngxScrollToOffsetMap
      .set(480, 100)
      .set(768, 200)
      .set(1240, 300)
      .set(1920, 400);
  }

  toggleDestination() {
    this.ngxScrollToDestination =
      this.ngxScrollToDestination === "destination-1"
        ? "destination-2"
        : "destination-1";
  }
}

my.component.html

<button (click)="toggleDestination()">Toggle Destination</button>

<button
  [ngxScrollTo]="ngxScrollToDestination"
  [ngxScrollToEvent]="ngxScrollToEvent"
  [ngxScrollToDuration]="ngxScrollToDuration"
  [ngxScrollToEasing]="ngxScrollToEasing"
  [ngxScrollToOffset]="ngxScrollToOffset"
  [ngxScrollToOffsetMap]="ngxScrollToOffsetMap"
>
  Go to destination
</button>

<div id="destination-1">You've reached your first destination</div>

<div id="destination-2">You've reached your second destination</div>

Advanced Usage - Service

my.component.html

<button (click)="triggerScrollTo()">Go to destination</button>

<div id="custom-container">
  <div id="destination">You've reached your destination.</div>
</div>

my.service.ts

import { Injectable } from "@angular/core";
import {
  ScrollToService,
  ScrollToConfigOptions,
} from "@nicky-lenaers/ngx-scroll-to";

@Injectable()
export class MyService {
  constructor(private scrollToService: ScrollToService) {}

  triggerScrollTo() {
    /**
     * @see NOTE:1
     */
    const config: ScrollToConfigOptions = {
      container: "custom-container",
      target: "destination",
      duration: 650,
      easing: "easeOutElastic",
      offset: 20,
    };

    this.scrollToService.scrollTo(config);
  }
}

NOTE:1

The container property is an optional property. By default, ngxScrollTo searches for the first scrollable parent HTMLElement with respect to the specified target. This should suffice in most cases. However, if multiple scrollable parents reside in the DOM tree, you have the degree of freedom the specify a specific container by using the container property, as used in the above example.

Directive Attributes Map

Options Type Default
ngxScrollTo string | number | ElementRef | HTMLElement ''
ngxScrollToEvent ScrollToEvent click
ngxScrollToDuration number 650
ngxScrollToEasing ScrollToAnimationEasing easeInOutQuad
ngxScrollToOffset number 0
ngxScrollToOffsetMap ScrollToOffsetMap new Map()

Error Handling

In some occasions, one might misspell a target or container selector string. Even though ngx-scoll-to will not be able to initiate the scrolling animation, you can catch the internally generated error and handle it as you please on the Observable chain returned from the scrollTo method.

faulty.service.ts

import { Injectable } from "@angular/core";
import { ScrollToService } from "@nicky-lenaers/ngx-scroll-to";

@Injectable()
export class FaultyService {
  constructor(private scrollToService: ScrollToService) {}

  triggerScrollTo() {
    this.scrollToService
      .scrollTo({
        target: "faulty-id",
      })
      .subscribe(
        (value) => {
          console.log(value);
        },
        (err) => console.error(err) // Error is caught and logged instead of thrown
      );
  }
}

Directive Attribute Map Details

[ngxScrollTo]

This value specifies the ID of the HTML Element to scroll to. Note the outer double quotes "" and the inner single quotes '' in the above example(s). This enables you to dynamically set the string value based on a class property of your Component.

[ngxScrollToEvent]

This value controls to event on which to trigger the scroll animation. Allowed values are:

  • click
  • mouseenter
  • mouseover
  • mousedown
  • mouseup
  • dblclick
  • contextmenu
  • wheel
  • mouseleave
  • mouseout

[ngxScrollToDuration]

This value controls to duration of your scroll animation. Note that this value is in milliseconds.

[ngxScrollToEasing]

This value controls a named easing logic function to control your animation easing. Allowed values are:

  • easeInQuad
  • easeOutQuad
  • easeInOutQuad
  • easeInCubic
  • easeOutCubic
  • easeInOutCubic
  • easeInQuart
  • easeOutQuart
  • easeInOutQuart
  • easeInQuint
  • easeOutQuint
  • easeInOutQuint
  • easeOutElastic

[ngxScrollToOffset]

This value controls the offset with respect to the top of the destination HTML element. Note that this value is in pixels.

[ngxScrollToOffsetMap]

This value allows you to control dynamic offsets based on the width of the device screen. The Map get's iterated over internally in a sequential fashion, meaning you need to supply key values in the order from low to high. The key of the Map defines the width treshold. The value of the Map defines the offset. Note that this value is in pixels.

Contributing

Please see Contributing Guidelines.

Code of Conduct

Please see Code of Conduct.

License

MIT

changelog

14.0.0 (2022-10-24)

BREAKING CHANGES

  • Angular version updated to v14

13.0.0 (2022-10-21)

Features

  • lib: support for angular v13 (cc69c0b)

9.0.0 (2020-04-23)

Bug Fixes

  • scroll to element with relative positioned parent (#124) (4d2c775)

Features

BREAKING CHANGES

  • lib: Rename properties to follow angular style guide

Before:

[ngx-scroll-to] [ngx-scroll-to-event] [ngx-scroll-to-duration] [ngx-scroll-to-easing] [ngx-scroll-to-offset] [ngx-scroll-to-offset-map]

After:

[ngxScrollTo] [ngxScrollToEvent] [ngxScrollToDuration] [ngxScrollToEasing] [ngxScrollToOffset] [ngxScrollToOffsetMap]

  • core: Fix lint

3.0.1 (2019-08-13)

Bug Fixes

  • incorrect package version (abd2e97)

3.0.0 (2019-08-13)

Bug Fixes

  • failing install due to locked deps (214d57e)
  • lib: replace "... from 'rjx/index'" import by "...from 'rxjs'" (#115) (452a986)

Features

2.0.0 (2018-12-12)

Features

1.1.1 (2018-09-12)

Bug Fixes

  • include src in npm packages (1e44246)

1.1.0 (2018-09-11)

Features

1.0.0 (2018-06-04)

1.0.0-beta.0 (2018-06-04)

Features

BREAKING CHANGES

  • Support for Angular 5 is dropped as of v1.0.0 of this module. For Angular 5 proejcts, please see install guides in the README file.

0.6.1 (2018-05-10)

Bug Fixes

  • module: non-target offset inside nested container (#80) (292e479)

Features

0.6.0 (2018-02-13)

Features

  • module: scroll to offset instead of target (#67) (c72828a), closes #28

0.5.3 (2018-02-02)

Bug Fixes

  • module: compare position value to mapped offset value (#64) (a2f4792), closes #62

0.5.2 (2018-02-02)

Bug Fixes

  • module: e2e runs against compatible chrome version (#63) (293b458)

0.5.1 (2018-02-02)

Bug Fixes

  • module: prevent float to int comparison and use int to int (b7fa36c), closes #62

0.5.0 (2017-12-03)

Bug Fixes

Features

  • module: Catchable Errors for suppression and custom Error Handling (#57) (0af1208), closes #50
  • module: Support for Angular 5 (#54) (fe1da1f), closes #52
  • module: Support for Native Element in both Target and Container (#56) (8ed7b63), closes #22 #55

0.4.0 (2017-10-12)

Bug Fixes

  • module: Correctly calcuate ngx-scroll-to-offset value on inverse scrolling from bottom to top (7656e35), closes #29

Features

  • module: ScrollTo using a diferent Container Element and without event parameter (bf3612f), closes #27 #20

BREAKING CHANGES

  • module: The ScrollToConfig Interface has been renamed to ScrollToConfigOptions
  • module: The event parameter has been removed from scrollTo function, see README.md for updated examples

0.4.0-alpha.3 (2017-10-04)

0.4.0-alpha.2 (2017-10-04)

0.4.0-alpha.1 (2017-10-04)

Features

  • comments: Set Proper Commenting (db167a1)
  • github: Improved Issue Template (4da3eae)
  • github: Proper PR merging for Changelog Generation (c64b59d)

0.3.1 (2017-08-08)

0.3.0 (2017-08-08)

0.2.4 (2017-07-27)

0.2.3 (2017-07-23)

0.2.2 (2017-07-22)

0.2.1 (2017-07-21)

0.2.0 (2017-07-21)

0.1.7 (2017-07-20)

0.1.6 (2017-07-20)

0.1.5 (2017-07-20)

0.1.4 (2017-07-20)

0.1.3 (2017-07-20)

0.1.2 (2017-07-20)

0.1.1 (2017-07-20)

0.1.0 (2017-07-20)

0.0.2 (2017-07-20)