npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng2-simple-page-scroll

v2.0.0

Published

Pure angular2 directive for instant jumping to html elements

Downloads

104

Readme

npm version Code Climate MIT license Dependency Status devDependency Status

ng2-simple-page-scroll

Animated "scroll to element" functionality written in pure angular2 with no additional dependencies

Features

  • easy-to-use directive: scroll to the top edge of an element referenced in the href-attribute (href="#mytarget) just by adding simplePageScroll directive
  • service usage: trigger instant scrolls from your component or when server responds
  • works across routes (scrolls to target element as soon as the routing has finished)

Table of contents

Setup

First you need to install the npm module:

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

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

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

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

Finally you need to specify how your application should load the ng2-simple page-scroll library:

Angular2 modules

All the compiled JavaScript files use ES2015 module format, so they are ready for usage with RollupJS. However, you cannot use them with SystemJS.

.metadata.json files are generated for usage with Angular2 AoT compiler.

SystemJS

UMD bundles are available for SystemJS loading. Example:

System.config({
    paths: {
        'npm:': 'node_modules/'
    },
    map: {
        app: 'app',

        '@angular/core'   : 'npm:@angular/core/bundles/core.umd.js',
        '@angular/common' : 'npm:@angular/common/bundles/common.umd.js',
        // further angular bundles...

        'ng2-simple-page-scroll/ng2-simple-page-scroll': 'npm:ng2-simple-page-scroll/bundles/ng2-simple-page-scroll.umd.js',

        rxjs: 'npm:rxjs',
    },
    packages: {
        app : {defaultExtension: 'js', main: './main.js'},
        rxjs: {defaultExtension: 'js'}
    }
});
import {Ng2SimplePageScrollModule} from 'ng2-simple-page-scroll/ng2-simple-page-scroll';

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

Usage

Directive

In your template you may add the simplePageScroll attribute to elements with an href attribute pointing towards an id on the same page (e.g. #theId).


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

Service

You may use the service for programmatic instant scrolls to HTML elements. Possible use cases are server responses or after content initialization.

Start by obtaining a reference to the SimplePageScrollService instance by adding it to your component's constructor. The SimplePageScrollService offers a simple scrollToElement() function that accepts an element #id or a reference to an HTMLElement.

@Component({
    template: `
        <p>Main content</p>
        <!-- Further content here -->
        <h2 id="head2">Part in a container</h2>
        <div #container>
            <p>Container content</p>
            <h3 id="head3">Heading</h3>
        </div>`
})
export class MyComponent {

     constructor(private simplePageScrollService: SimplePageScrollService, @Inject(DOCUMENT) private document: Document) {
     }

     public goToHead2(): void {
         this.simplePageScrollService.scrollToElement("#head2", /* optional offset */);
         this.simplePageScrollService.scrollToElement(elementReference, /* optional offset */);
     };
 }

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 Option | Type | Default | Description | | ---------------------- | ----------- | ------------ |-------------- | | 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;
    }
}

Directive API

Additional attributes may be set on an DOM element using the simplePageScroll directive for customization. They take precedence over the default settings specified in SimplePageScrollConfig class. Thereby it is possible to have all page scrolls have a default offset, but a specific one should have a different offset.

PageScroll properties

| Attribute | Type | Default | Description | | ------------------------- | ----------- | ------------ |-------------- | | simplePageScroll | | | Attribute adding scroll behavior when the click-event happens on the element. | 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).

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 (true) or not (false). Possible reasons for false: target not found.

Example

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

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

Overriding all possible properties. doSmth() is defined in the component

 <a simplePageScroll [pageScrollOffset]="0" (pageScrollFinish)="doSmth($event)" href="#theanchor">Visit</a>

    doSmth(scrollSuccesful: boolean): void {
        if (scrollSuccesful) {
            console.log('Yeah, we scrolled');
        } else {
            console.log('Ohoh, something prevented the scroll');
        }
    }