All Projects → nicky-lenaers → Ngx Scroll To

nicky-lenaers / Ngx Scroll To

Licence: mit
Scroll to any element to enhance scroll-based features in you app. Works for Angular 4+, both AoT and SSR. No dependencies.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ngx Scroll To

Ngx Daterangepicker Material
Pure Angular 2+ date range picker with material design theme, a demo here:
Stars: ✭ 169 (-37.17%)
Mutual labels:  angular2, angular4, ngx
Angular2 Carousel
An lightweight , touchable and responsive library to create a carousel for angular 2 / 4 / 5
Stars: ✭ 26 (-90.33%)
Mutual labels:  angular2, angular4, ngx
Moveto
A lightweight scroll animation javascript library without any dependency
Stars: ✭ 2,746 (+920.82%)
Mutual labels:  scroll, scrolling, smooth-scrolling
Angulartics2
Vendor-agnostic analytics for Angular2 applications.
Stars: ✭ 963 (+257.99%)
Mutual labels:  angular2, angular4, ngx
Ngx File Drop
Angular 11 file and folder drop library
Stars: ✭ 220 (-18.22%)
Mutual labels:  angular2, angular4, ngx
Mac Mouse Fix
Mac Mouse Fix - A simple way to make your mouse better.
Stars: ✭ 362 (+34.57%)
Mutual labels:  scroll, scrolling, smooth-scrolling
really-smooth-scroll
A script that smoothen scrolling in the browser
Stars: ✭ 21 (-92.19%)
Mutual labels:  scrolling, scroll, smooth-scrolling
angular2-cookie-law
Angular2+ component that provides a banner to inform users about cookie law
Stars: ✭ 38 (-85.87%)
Mutual labels:  angular2, angular4
ng2-timezone-selector
A simple Angular module to create a timezone selector using moment-timezone.
Stars: ✭ 12 (-95.54%)
Mutual labels:  angular2, angular4
angular-progress-bar
This component allow you to easy incorporate progress-bar to angular/ionic project, providing binding and color options
Stars: ✭ 26 (-90.33%)
Mutual labels:  angular2, angular4
Angular-Firebase-Sortable-Table
Angular Firebase Sortable Table is a module that makes tables creation with firebase an easy task.
Stars: ✭ 28 (-89.59%)
Mutual labels:  angular2, angular4
ncg-crud-ngx-md
Angular 4+ Material Design CRUD/Admin app by NinjaCodeGen http://DNAfor.NET
Stars: ✭ 36 (-86.62%)
Mutual labels:  angular2, angular4
volx-recyclerview-fast-scroll
An easy to use implementation for fast scroll recyclerview
Stars: ✭ 34 (-87.36%)
Mutual labels:  scrolling, scroll
scrollparent.js
A function to get the scrolling parent of an html element.
Stars: ✭ 51 (-81.04%)
Mutual labels:  scrolling, scroll
angular-material-datatransfer
A HTML5 datatransfer UI for handling upload and download of multiple simultaneous files.
Stars: ✭ 13 (-95.17%)
Mutual labels:  angular2, angular4
ngx-redux-ui-management-recipes
Recipes for managing the UI layout of an application using Redux in Angular
Stars: ✭ 39 (-85.5%)
Mutual labels:  angular2, angular4
angular2-crud-auth-routing
Sample Angular (2.x, 4.x) project with Contacts CRUD + routing + simple authentication
Stars: ✭ 60 (-77.7%)
Mutual labels:  angular2, angular4
ngmodule-viz
Visualize the dependencies between the NgModules in your Angular 2+ application
Stars: ✭ 35 (-86.99%)
Mutual labels:  angular2, angular4
Ngx Smart Modal
Modal/Dialog component crafted for Angular
Stars: ✭ 256 (-4.83%)
Mutual labels:  angular2, angular4
angular4-seed-starter
An angular4 starter with webpack2+aot+lazyload+hmr+scss(个人搭的angular4 starter,使用webpack2,包含aot、lazyload、scss、热替换功能等等)
Stars: ✭ 23 (-91.45%)
Mutual labels:  angular2, angular4

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 9!

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 8 and 9

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

Angular 7

$ npm install @nicky-lenaers/[email protected]'2'

Angular 6

$ npm install @nicky-lenaers/[email protected]'1'

Angular <= 5.x

$ npm install @nicky-lenaers/[email protected]'<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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].