GithubHelp home page GithubHelp logo

bensgroi / ng2-simple-page-scroll Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bbottema/ng2-simple-page-scroll

0.0 1.0 0.0 51 KB

Angular2 lightweight support for instant navigation to anchors within a view

License: MIT License

JavaScript 34.80% TypeScript 65.20%

ng2-simple-page-scroll's Introduction

npm version Code Climate MIT license Dependency Status devDependency Status

ng2-simple-page-scroll

Instant "Jump to element" functionality rewritten in a pure angular2 directive with no additional dependencies

Features

  • jump to the top edge of an element referenced in the href-attribute (href="#mytarget)
  • works across routes (scrolls to target element as soon as the routing has finished)

Setup

First you need to install the npm module:

npm install ng2-simple-page-scroll --save

Then import the directive and add it to the directives array of your component. In your template you may now add the simplePageScroll attribute to elements with an href attribute pointing towards an anchor on the same page (e.g. #anchor).

import {SimplePageScroll} from 'ng2-simple-page-scroll';


@Component({
   ...
   template: `...
        <a simplePageScroll href="#myanchor">Go there</a>
   ...`,
    directives: [SimplePageScroll]
})
export class MyComponent  {
}

Configuration

The class SimplePageScrollConfig offers static properties to be manipulated to configure the default behavior. Override the respective properties to change all page scroll-animation defaults.

Configuration properties

  • defaultScrollOffset (?: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)

Example

import {SimplePageScrollConfig} from 'ng2-simple-page-scroll';

export class AppComponent {
    constructor() {
        SimplePageScrollConfig.defaultScrollOffset = 50;
    }
}

API

The property may be set on individual elements as well. They take precedence over the default settings specified in SimplePageScrollConfig class.

SimplePageScroll properties

  • simplePageScroll - Attribute to add scroll-animation behavior when the click-event happens to an existing element.
  • simplePageScrollOffset (?: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)

Example

Most basic example:

 <a simplePageScroll href="#myanchor">Go there</a>

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

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

Overriding offset property.

 <a simplePageScroll [simplePageScrollOffset]="0" href="#theanchor">Visit</a>

License

MIT

ng2-simple-page-scroll's People

Contributors

bbottema avatar bensgroi avatar nolanus avatar nonameprovided avatar

Watchers

 avatar

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.