Giter Club home page Giter Club logo

ngx-scrolltop's Introduction

Angular ScrollTop Button

npm version NPM Downloads Join the chat at https://gitter.im/ngx-scrolltop/Lobby volkswagen status

Lightweight, Material Design inspired button for scroll-to-top of the page. No dependencies. Pure Angular!

Angular 17, Angular Universal (SSR) a Standalone Components compatible

Just hit the button to smoothly scroll back to the top of the page. Here's the demo.

  • Lightweight (~2 kB gzipped)
  • Angular 17 compatible
  • Standalone Components compatible
  • Signals compatible
  • No dependencies! (only smooth scroll-behavior polyfill for iOS)
  • Material Design inspired
  • @angular/material compatible (example)
  • Component or directive way
  • Smoothly animated
  • a11y ready
  • Highly customizable options...
  • Angular 5+ compatible

Demo animation

🎯 Demo (example)

Watch this demo page

Or play with it live on stackblitz.com/edit/ngx-scrolltop

📦 Install

You're not using the latest version of Angular? See our Angular compatibility instructions…

Via npm or yarn

npm install ngx-scrolltop --save # for lastest Angular only! See our compatibility table

# yarn add ngx-scrolltop

Compatibility

Angular version ngx-scrolltop Install
ng17 v17.x.x ng add ngx-scrolltop@latest
ng16 v6.x.x ng add ngx-scrolltop@6
ng15 v6.x.x ng add ngx-scrolltop@6
ng14 v4.x.x ng add ngx-scrolltop@4
ng13 v4.x.x ng add ngx-scrolltop@4
ng12 v4.x.x ng add ngx-scrolltop@4
ng5 – ng11 v2.x.x ng add ngx-scrolltop@2

🗜️ Setup and usage (Standalone or Modules)

You have some options how to use this library. As a component or as a directive.

You can also use it as a Standalone component (new angular way) or as a module (the original angular way).

Standalone Component

In your root standalone component (e.g. app.component.ts) you need to add NgxScrollTopComponent into your imports array.

...
+ import { NgxScrollTopComponet } from 'ngx-scrolltop';
...

@Component({
  selector: 'app',
  standalone: true,
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  imports: [
+   NgxScrollTopComponent
  ],
})
export class AppComponent { }

And then in app.component.html you just need to add your new button. Usually at the end of your file.

<ngx-scrolltop></ngx-scrolltop>

See options and examples for more details...

Standalone Directive

In your root standalone component (e.g. app.component.ts) you need to add NgxScrollTopDirective into your imports array.

...
+ import { NgxScrollTopDirective } from 'ngx-scrolltop';
...

@Component({
  selector: 'app',
  standalone: true,
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  imports: [
+   NgxScrollTopDirective
  ],
})
export class AppComponent { }

And then in app.component.html you just need to create your new custom element... (e.g. <span>, <div>, <button>, ...)

<span ngxScrollTop class="my-custom-element-with-my-style__no-lib-style-applied-here">
  ↑ My Custom Element. ↑
</span>

Important note: No style applied, everything is up to you. Of course I recommend position: fixed, ...

See more in examples

Modules (the original Angular way)

This is the original Angular way how to import libraries (via modules). You can use it as a component or as a directive as well.

In your root module (e.g. app.module.ts) you need to add NgxScrollTopModule into your imports array.

...
+ import { NgxScrollTopModule } from 'ngx-scrolltop';
...

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

Next steps you can see above in Standalone Component or Standalone Directive or in examples.

⚙️ Options

Component

Option Type Default Description
mode 'smart' | 'classic' 'classic' Smart mode shows button only when you scroll more than two screens down and then you will try to go back to top. Classic mode shows button immediately when you scroll at least one screen down.
backgroundColor string #212121 Background color (you can use all values for backgroud-color css property). You can override theme color
symbolColor string #fafafa Symbol color (you can use all values for fill svg property). You can override theme color
size number 40 Button size [in pixels]. (Symbol will be resized automatically)
symbol string You can use utf8 chars for customizing symbol. For example: Removed in v2.0.0. Use content projection. See example here
position 'left' | 'right' 'right' Left or right, that is the question...
theme NgxScrollTopTheme 'gray' Choose from official Material color themes

Symbol

You can use content projection for your own symbol.

<ngx-scrolltop></ngx-scrolltop>

Or you can even use your own components or fonts (e.g. fontAwesome)

<ngx-scrolltop>
  <i class="fa fa-arrow-up"></i>
</ngx-scrolltop>

Directive

Option Type Default Description
[ngxScrollTopMode] 'smart' | 'classic' 'classic' Smart mode shows button only when you scroll more than two screens down and then you will try to go back to top.Classic mode shows button immediately when you scroll at least one screen down.

🎨 Examples

Advanced example (component)

app.component.html

<ngx-scrolltop
  [size]="50"
  backgroundColor="#33691e"
  symbolColor="#fff"
  mode="smart"
  position="left"
  theme="pink"
></ngx-scrolltop>

Angular Material example (directive)

@angular/material required

app.component.html

<button
  ngxScrollTop
  [ngxScrollTopMode]="'smart'"
  class="my-custom-style"
  color="primary"
  mat-mini-fab
>
  top
</button>

app.component.scss

.my-custom-style {
  position: fixed;
  right: 20px;
  bottom: 20px;
}

🔌 Dependencies

Pure Angular! (with smooth scroll-behaviour polyfill for iOS)

🙋 FAQ

Old version of Angular

Error

If you are using older Angular (5 – 11) you will see this error on build or start:

ERROR in node_modules/ngx-scrolltop/lib/ngx-scrolltop.core.service.d.ts:13:21 - error TS2694: Namespace '"/Users/user/projects/my-project/node_modules/@angular/core/core"' has no exported member 'ɵɵFactoryDeclaration'.

Solution

Easy! Use compatibility version of this library npm install ngx-scrolltop@2 --save

See our Angular compatibility instructions…

Error

If you are using Angular 14 with latest ngx-scrolltop library, you will see this error on build or start:

Error: node_modules/ngx-scrolltop/lib/ngx-scrolltop.component.d.ts:21:18 - error TS2707: Generic type 'ɵɵComponentDeclaration' requires between 7 and 8 type arguments.

21     static ɵcmp: i0.ɵɵComponentDeclaration<NgxScrollTopComponent, "ngx-scrolltop", never, { "backgroundColor": "backgroundColor"; "symbolColor": "symbolColor"; "size": "size"; "symbol": "symbol"; "position": "position"; "theme": "theme"; "mode": "mode"; }, {}, never, ["*"], false, never>;
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Error: node_modules/ngx-scrolltop/lib/ngx-scrolltop.directive.d.ts:17:18 - error TS2707: Generic type 'ɵɵDirectiveDeclaration' requires between 6 and 8 type arguments.

17     static ɵdir: i0.ɵɵDirectiveDeclaration<NgxScrollTopDirective, "[ngxScrollTop]", never, { "mode": "ngxScrollTopMode"; }, {}, never, never, false, never>;

Solution

Easy! Use compatibility version of this library npm install ngx-scrolltop@4 --save

See our Angular compatibility instructions…

⭐️ Show your support

Give a ⭐️ if this project helped you!

Or if you are brave enough consider making a donation for some 🍺 or 🍵 ;)

📝 License

Copyright © 2023 Lukas Bartak

Proudly powered by nature 🗻, wind 💨, tea 🍵 and beer 🍺 ;)

All contents are licensed under the MIT license.

ngx-scrolltop's People

Contributors

bartholomej avatar dependabot[bot] avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ngx-scrolltop's Issues

Scroll bottom feature

Just like we have scroll to top. Can we have scroll to bottom and same button works as both.

No works with Angular 14 lastest

I cant use your library after update to lastest@angular14

Your global Angular CLI version (15.0.4) is greater than your local version (14.2.10). The local Angular CLI version is used.

To disable this warning use "ng config -g cli.warnings.versionMismatch false".

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 14.2.10
Node: 16.17.0
Package Manager: npm 8.15.0 
OS: darwin arm64

Angular: 14.2.12
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router, service-worker

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1402.10
@angular-devkit/build-angular   14.2.10
@angular-devkit/core            14.2.10
@angular-devkit/schematics      14.2.10
@angular/cli                    14.2.10
@schematics/angular             14.2.10
rxjs                            6.6.7
typescript                      4.8.4

I am getting the following error when try to run ng serve

Error: node_modules/ngx-scrolltop/lib/ngx-scrolltop.component.d.ts:21:18 - error TS2707: Generic type 'ɵɵComponentDeclaration' requires between 7 and 8 type arguments.

21     static ɵcmp: i0.ɵɵComponentDeclaration<NgxScrollTopComponent, "ngx-scrolltop", never, { "backgroundColor": "backgroundColor"; "symbolColor": "symbolColor"; "size": "size"; "symbol": "symbol"; "position": "position"; "theme": "theme"; "mode": "mode"; }, {}, never, ["*"], false, never>;
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Error: node_modules/ngx-scrolltop/lib/ngx-scrolltop.directive.d.ts:17:18 - error TS2707: Generic type 'ɵɵDirectiveDeclaration' requires between 6 and 8 type arguments.

17     static ɵdir: i0.ɵɵDirectiveDeclaration<NgxScrollTopDirective, "[ngxScrollTop]", never, { "mode": "ngxScrollTopMode"; }, {}, never, never, false, never>;
                   

I think the version of the library is correct but no works

% npm list ngx-scrolltop
[email protected]
└── [email protected]

my package.json

{
  "name": "myapp",
  "version": "2.2.1",
  "scripts": {
    "ng": "ng",
    "start": "ng s -o",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "postinstall": "ng build",
    "bundle-analizer": "ng build --stats-json && npx webpack-bundle-analyzer dist/myapp/stats.json",
    "bundle-dist-app": "ng build && node bundle-dist-config.js && start .",
    "sonar": "sonar-scanner",
    "test-code": "concurrently --raw \"ng test --watch --code-coverage\" \"node open-file-explorer\" ",
    "test-code-coverage": "ng test --watch --code-coverage",
    "bundle-dist-app-ci": "ng build && node bundle-dist-config.js",
    "test-code-ci": "npm run test -- --browsers ChromeHeadless --watch false --code-coverage false",
    "compodoc:build": "compodoc -p tsconfig.doc.json",
    "compodoc:build-and-serve": "compodoc -p tsconfig.doc.json -s",
    "compodoc:serve": "compodoc -s"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^14.2.12",
    "@angular/common": "^14.2.12",
    "@angular/compiler": "^14.2.12",
    "@angular/core": "^14.2.12",
    "@angular/forms": "^14.2.12",
    "@angular/platform-browser": "^14.2.12",
    "@angular/platform-browser-dynamic": "^14.2.12",
    "@angular/router": "^14.2.12",
    "@angular/service-worker": "^14.2.12",
    "animate.css": "^4.1.1",
    "bootstrap": "^5.2.3",
    "bootswatch": "^5.1.3",
    "colors": "^1.4.0",
    "concurrently": "^7.2.2",
    "core-js": "^2.6.11",
    "date-fns": "^2.27.0",
    "express": "^4.17.1",
    "karma-coverage": "^2.2.0",
    "karma-mocha-reporter": "^2.2.5",
    "ng-lazyload-image": "^9.1.0",
    "ng-navigator-share": "^1.0.4",
    "ngx-masonry": "^13.0.0",
    "ngx-order-pipe": "^2.2.0",
    "ngx-pagination": "^5.0.0",
    "ngx-scrolltop": "^5.0.2",
    "ngx-stars": "^1.5.1",
    "ngx-toastr": "^12.1.0",
    "open": "^8.4.0",
    "rxjs": "^6.6.7",
    "tslib": "^1.11.1",
    "webpack-bundle-analyzer": "^4.5.0",
    "zip-lib": "^0.7.2",
    "zone.js": "^0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.2.10",
    "@angular/cli": "^14.2.10",
    "@angular/compiler-cli": "^14.2.12",
    "@angular/language-service": "^14.2.12",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "^2.0.8",
    "@types/node": "^12.12.34",
    "jasmine-core": "^4.1.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^6.3.19",
    "karma-chrome-launcher": "^3.1.1",
    "karma-coverage-istanbul-reporter": "^2.1.1",
    "karma-jasmine": "^4.0.2",
    "karma-jasmine-html-reporter": "^1.7.0",
    "protractor": "^7.0.0",
    "sonar-scanner": "^3.1.0",
    "ts-node": "~7.0.0",
    "tslint": "~6.1.3",
    "typescript": "~4.8.4"
  }
}

Timeline for Angular 17 Support

Quick question, but I have a project we're upgrading to Angular 17 that uses this package. It's the only one that doesn't have Angular 17 support. I was wondering if there was a timeline to get that added.

Thanks!

Does it work with Angular 7.2.3?

Tried with latest version. Got the below error.

ERROR in node_modules/ngx-scrolltop/lib/ngx-scrolltop.core.service.d.ts(6,17): error TS1086: An accessor cannot be declared in an ambient context.

Support standalone component to decrease my bundle size

Is your feature request related to a problem? Please describe.
NgxScrollTopModule is increasing my bundle size for no reason. I just want to use NgxScrollTopDirective without importing NgxScrollTopComponent.

Describe the solution you'd like
Please make NgxScrollTopDirective & NgxScrollTopComponent standalone... NgxScrollTopModule can be still available (Same like CommonModule in @angular/core)

Describe alternatives you've considered
None

Additional context
None

Can't install on Angular 14

Package can't be installed on Angular 14 as dependencies are unmet :

ng add ngx-scrolltop@latest
ℹ Using package manager: npm
⚠ Package has unmet peer dependencies. Adding the package may not succeed.

The package ngx-scrolltop@latest will be installed and executed.
Would you like to proceed? Yes
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: @angular/[email protected]
npm ERR! node_modules/@angular/common
npm ERR!   @angular/common@"^14.0.5" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer @angular/common@">=12 <14" from [email protected]
npm ERR! node_modules/ngx-scrolltop
npm ERR!   ngx-scrolltop@"4.1.2" from the root project
ng version

Angular CLI: 14.0.6
Node: 16.15.1
Package Manager: npm 8.14.0
OS: linux x64

Angular: 14.0.6
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1400.6
@angular-devkit/build-angular   14.0.6
@angular-devkit/core            14.0.6
@angular-devkit/schematics      14.0.6
@angular/cdk                    14.0.5
@angular/flex-layout            14.0.0-beta.40
@angular/material               14.0.5
@schematics/angular             14.0.6
rxjs                            7.5.6
typescript                      4.7.4

[ngxScrollTopMode] is ignored

I try to use my custom button like that:
<span ngxScrollTop [ngxScrollTopMode]="'classic'"> TOP </span>

But ngxScrollTopMode does't work at all, the btn is ALWAYS visible, also if the page is barely scrollable ( if I click the btn I move to the top of the page, so this works good).
Instead,
<ngx-scrolltop mode="classic">↑</ngx-scrolltop>
works perfectly.

I'm using:
"ngx-scrolltop": "^4.1.2",
"@angular/cdk": "^13.3.5",
"@angular/core": "^13.3.5",

What I am doing wrong?
Thank you

Scroll to top icon not appearing in on iphone 6.

Apologies, I don't have any more devices to test on than iphone 5C (IOS version 10.3.3) but it works on IOS 14, below is my configuration of my app.component.html in my Angular 10 app.

<router-outlet></router-outlet>
<ngx-scrolltop
  [size]="50"
  backgroundColor="#2E5299"
  symbolColor="#ffffff">
</ngx-scrolltop>
<app-footer></app-footer>

Using CSS vars to theme color

Is possible do this?

<ngx-scrolltop [size]="55" color="var(--dark)"></ngx-scrolltop>

Because Ive tried but doesnt work

😉

Disable scrolltop animation on page navigation?

Is there a way to disable the scrollTop animation on route changes? I want the page to snap to the top but its animating the scroll to the top which is making for a poor user experience.

Not working with Angular 12

I have tried

ng add ngx-scrolltop

And it was giving error for ng-add support.

So then I installed manually

npm install ngx-scrolltop --save

And then added in app.component.html
<ngx-scrolltop></ngx-scrolltop>

and made sure app.module.ts has the NgxScrollTopModule with import.

And I have tried checking in couple of ways and no matter I do after scrolling down nothing shows up.

Add a window selector

Scrolling works perfectly when using a single window.

However, if there is a <div> element scrolling in the middle of the page (a list with messages, max-height 200px, scrolling on y), the scrolling button won't be shown while you scroll inside the list of messages.

Is there a way to implement a window selector? So it can refer to the parent y-scroll instead of the window one?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.