All Projects → Nolanus → Ngx Page Scroll

Nolanus / Ngx Page Scroll

Licence: mit
Animated scrolling functionality for angular written in pure typescript

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ngx Page Scroll

React Scrollchor
A React component for scroll to `#hash` links with smooth animations
Stars: ✭ 141 (-66.59%)
Mutual labels:  animate, scroll
React Scrolllock
🔒 Prevent scroll on the <body />
Stars: ✭ 393 (-6.87%)
Mutual labels:  scroll
Jump.js
A modern smooth scrolling library.
Stars: ✭ 3,459 (+719.67%)
Mutual labels:  scroll
Jxpagelistview
高仿闲鱼、转转、京东、中央天气预报等主流APP列表底部分页滚动视图
Stars: ✭ 377 (-10.66%)
Mutual labels:  scroll
Dragsloplayout
A UI library for Android
Stars: ✭ 354 (-16.11%)
Mutual labels:  scroll
Scrollreveal
Animate elements as they scroll into view.
Stars: ✭ 20,264 (+4701.9%)
Mutual labels:  scroll
Vue Scrollama
Vue component to easily setup scroll-driven interactions (aka scrollytelling)
Stars: ✭ 326 (-22.75%)
Mutual labels:  scroll
Scrollload
scroll bottom load more data pull refresh 滚动到底部加载更多数据 下拉刷新
Stars: ✭ 411 (-2.61%)
Mutual labels:  scroll
React Native Parallax Scroll
Parallax scroll view for react-native
Stars: ✭ 385 (-8.77%)
Mutual labels:  scroll
Hc Sticky
JavaScript library that makes any element on your page visible while you scroll.
Stars: ✭ 375 (-11.14%)
Mutual labels:  scroll
Mac Mouse Fix
Mac Mouse Fix - A simple way to make your mouse better.
Stars: ✭ 362 (-14.22%)
Mutual labels:  scroll
Ngx Scrollbar
Custom overlay-scrollbars with native scrolling mechanism
Stars: ✭ 355 (-15.88%)
Mutual labels:  scroll
React Scrollspy
🔯 react scrollspy component
Stars: ✭ 382 (-9.48%)
Mutual labels:  scroll
Uos
🐭 A tiny 250b scroll listener with progress.
Stars: ✭ 349 (-17.3%)
Mutual labels:  scroll
Notebook
my notebook 📒
Stars: ✭ 405 (-4.03%)
Mutual labels:  scroll
React Awesome Resume
a resume by using Luy/React
Stars: ✭ 337 (-20.14%)
Mutual labels:  animate
Xlcardswitch
iOS 利用余弦函数特性实现可以居中放大的图片浏览工具
Stars: ✭ 361 (-14.45%)
Mutual labels:  scroll
Boomjs
JavaScript实现一个有趣的浏览器图片爆炸动画效果
Stars: ✭ 379 (-10.19%)
Mutual labels:  animate
Use Scroll Position
Use scroll position ReactJS hook done right
Stars: ✭ 414 (-1.9%)
Mutual labels:  scroll
Lethargy
Distinguish between scroll events initiated by the user, and those by inertial scrolling
Stars: ✭ 407 (-3.55%)
Mutual labels:  scroll

ngx-page-scroll npm version MIT license

Animated scrolling functionality for angular written in pure typescript with no additional dependencies

Build Status Dependencies Status devDependencies Status peerDependencies Status Greenkeeper badge Codacy Badge

Breaking Change: Starting with v6 this library consists of two modules: ngx-page-scroll-core (the service) and ngx-page-scroll (the directive). ➡ How to upgrade

Features

  • flexible: trigger scroll animations after component load, server response, etc.
  • easy-to-use directive: scroll to an element referenced in the href-attribute (href="#mytarget) just by adding pageScroll directive
  • customizable: adjust duration, offset or whether scrolling stops if the user interrupts (read more)
  • use custom easing functions to calculate the scroll position over time
  • works across routes (scrolls to target element as soon as the routing has finished) and in both directions (horizontal/vertical)

Table of contents

Version compatibility

Install later versions in case your app is not running the very latest angular version.

ngx-page-scroll/ngx-page-scroll-core version compatible angular version Documentation
v7.x v11, v10, v9, v8 README
v6.x v8, v7 README
v5.x v6 README
v4.x v5, v4 README

Service

Setup

First you need to install the core npm module:

npm install ngx-page-scroll-core --save

Then add the NgxPageScrollModule to the imports array of your application module:

import { NgxPageScrollCoreModule } from 'ngx-page-scroll-core';

@NgModule({
    imports: [
        /* Other imports here */
        NgxPageScrollCoreModule
        ]
})
export class AppModule {
}

Usage

Using the PageScrollService#scroll method you may trigger scroll animations. Provide an options object that provides a reference to the document and the scroll target. Additional properties are optional.

import { Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { PageScrollService } from 'ngx-page-scroll-core';

export class MyComponent {
 constructor(private pageScrollService: PageScrollService, @Inject(DOCUMENT) private document: any) {
 }
   
 ngOnInit(): void {
  this.pageScrollService.scroll({
    document: this.document,
    scrollTarget: '.theEnd',
  });
 }
}

Note: The scroll() method is a shorthand from creating a PageScrollInstance (an object encapsulating all information relevant for performing a scroll animation) using PageScrollService#create and starting it using the PageScrollService#start method.

Configuration

When importing the PageScrollCoreModule one can provide option overrides:

imports: [
  ...
  NgxPageScrollCoreModule.forRoot({duration: 2500, easingLogic: ...}),
]

Check the PageScrollConfig interface for possible options and their impact. The default values may be found in the defaultPageScrollConfig object.

Directive

For ease of use a directive pageScroll exists, which allows you to quickly add scroll animations to your angular app by adding a property to your existing HTML a-tags. It can also work cross-routes, meaning it will start the scroll animation after the target route has been loaded. It utilizes the ngx-page-scroll-core module for that, thus requires it as a peer dependency.

Setup

First you need to install the directive npm module:

npm install ngx-page-scroll --save

Then add the NgxPageScrollModule to the imports array of your application module:

import { NgxPageScrollModule } from 'ngx-page-scroll';

@NgModule({
    imports: [
        /* Other imports here */
        NgxPageScrollModule
        ]
})
export class AppModule {
}

Usage

In your template you may add the pageScroll attribute to elements with an href attribute pointing towards an id on the same page (e.g. #theId). The onClick event will be interrupted and the scroll animation starts. Alternatively you may set the optional pageScrollTarget property to a valid css selector to specify the target element to scroll to.

@Component({
   ...
   template: `...
        <a pageScroll >Take me to the awesomeness</a>
        <!-- Further content here -->
        <h2 id="awesomePart">This is where the awesome happens</h2>
   ...`,
})
export class MyComponent {
}

Directive API

Additional attributes may be set on an DOM element using the pageScroll directive for customization. They take precedence over the default settings specified in PageScrollConfig class. Thereby it is possible to have all page scroll-animations last e.g. 2 seconds, but a specific one should be performed with a custom easing function and a duration of only 1 second.

PageScroll properties

Attribute Type Default Description
pageScroll Attribute adding scroll-animation behavior when the click-event happens on the element.
pageScrollTarget string Optional attribute to set the element that should be scrolled to. Takes precedence over the ´href´-value
pageScrollHorizontal boolean false Whether the scroll should happen in vertical direction (false, default) or horizontal (true).
pageScrollOffset number 0 Pixels to offset from the top of the element when scrolling to (positive value = scrolling will stop given pixels atop the target element).
pageScrollDuration number 1250 Duration in milliseconds the whole scroll-animation should last.
pageScrollSpeed number - Speed in Pixel/Second the animation should take. Only applied if no duration is set.
pageScrollInView boolean true Whether the scroll animation should happen even when the scroll target is already inside the view port (true). Set to false to skip scroll animation if target is already in view.
pageScrollInterruptible boolean true Whether the scroll animation should stop if the user interferes with it (true) or not (false).
pageScrollAdjustHash boolean false Whether the routes hash/fragment should be updated to reflect to section that has been scrolled to
pageScrollEasing EasingLogic linearEasing Easing method to be used while calculating the scroll position over time (default is linear easing).

PageScroll events

Event Type Description
pageScrollFinish boolean Fired when the scroll-animation stops. Emits a boolean value which indicates whether the scroll animation finished successfully and reached its target (true) or not (false). Possible reasons for false: target not found or interrupted due to another scroll animation starting or user interaction.

Example

The following example will check whether the route Home is currently loaded. If this is true, the scroll-animation will be performed with the default properties. If a different route is loaded, a subscription for route changes will be made and the scroll-animation will be performed as soon as the new route is loaded.

 <a pageScroll [routerLink]="['Home']" href="#myanchor">Go there</a>

Overriding all possible properties. doSmth() and myEasing are defined in the component

 <a pageScroll [pageScrollOffset]="0" [pageScrollDuration]="2000" [pageScrollEasing]="myEasing" [pageScrollInterruptible]="false" (pageScrollFinish)="doSmth($event)" href="#theanchor">Visit</a>
    public myEasing: EasingLogic = (t: number, b: number, c: number, d: number): number => {
      // easeInOutExpo easing
      if (t === 0) {
        return b;
      }
      if (t === d) {
        return b + c;
      }
      if ((t /= d / 2) < 1) {
        return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
      }
  
      return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
    }

    doSmth(reachedTarget: boolean): void {
        if (reachedTarget) {
            console.log('Yeah, we reached our destination');
        } else {
            console.log('Ohoh, something interrupted us');
        }
    }

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].