Giter VIP home page Giter VIP logo

ng2smoothscroll's Introduction

Angular2 smooth scroll

base onAngular smooth scroll

A pure-javascript library and set of directives to scroll smoothly to an element with easing. Easing support contributed by Willem Liu with code from Gaëtan Renaudeau.

No jQuery required.

Features

  • Exposes a service that scrolls the window to an element's location
  • Provides two directives that enable smooth scrolling to elements.
  • Clean: No classes are added, no jQuery is required, no CSS files or configuration is needed.
  • Scrolling within a custom container added in 2.0.0

Installation

// bower:
import { SmoothScrollToDirective, SmoothScrollDirective } from "ng2SmoothScroll";

// npm:
import { SmoothScrollToDirective, SmoothScrollDirective } from "ng2-smooth-scroll";
...
declarations[
	...
	SmoothScrollToDirective,
	SmoothScrollDirective,
	...
]

Bower

Install with bower with:

bower install ng2SmoothScroll

Npm

Install with npm with:

npm install ng2-smooth-scroll

Usage - As a directive

This module provides two directives:

smoothScroll:

Attribute. Scrolls the window to this element, optionally validating the expression inside scroll-if.

Example:

// Basic - The window will scroll to this element's position when compiling this directive
<div smoothScroll></div>

// With options
<div smoothScroll
	[duration]="800"
	[easing]="easeInQuint"
	[offset]="120"
	[middleAlign]="true"
	[callbackBefore]="aFunction(element)"
	[callbackAfter]="anotherFunction">
	{{...}}
</div>

// Inside a custom container
<div smoothScroll
	[duration]="800"
	[easing]="easeInQuint"
	[offset]="120"
	[callbackBefore]="aFunction(element)"
	[callbackAfter]="anotherFunction"
	[containerId]="container-id">
	{{...}}
</div>

// With condition
<div smoothScroll
	[scrollIf]="myExpression">
	{{...}}
</div>

// Inside ng-repeat
<div smoothScroll
	[scrollIf]="myExpression"
	[duration]="2500">
	{{...}}
</div>

####scrollTo:

Attribute. Scrolls the window to the specified element ID when clicking this element.

Example:

// Basic
<a href="#"
	[scrollTo]="'my-element-3'">
	Click me!
</a>

// Custom containers
<a href="#"
	[scrollTo]="'my-element-3'"
	[containerId]="custom-container-id">
	Click me!
</a>

// onClick for non-anchor tags
<div [scrollTo]="'my-element-3'"
	[containerId]="custom-container-id">
	Click me!
</div>

// With options
<button
	[scrollTo]="'elem-id5'"
	[duration]="1800"
	[middleAlign]="true"
	[callbackBefore]="aFunction(element)"
	[callbackAfter]="anotherFunction">
	Scroll to next page.
</button>

Options

duration

Type: Integer Default: 800

The duration of the smooth scroll, in miliseconds.

offset

Type: Integer Default: 0

The offset from the top of the page in which the scroll should stop.

easing

type: string default: easeInOutQuart

the easing function to be used for this scroll.

middleAlign

type: boolean default: false

Middle align the scrolled element

scrollOnClick

type: boolean default: false

(smoothScroll directive only) Scroll to element when it is clicked

callbackBefore

type: function default: function(element) {}

a callback function to run before the scroll has started. It is passed the element that will be scrolled to.

callbackAfter

type: function default: function(element) {}

a callback function to run after the scroll has completed. It is passed the element that was scrolled to.

containerId

type: string default: null

ID of the scrollable container which the element is a child of.

Easing functions

The available easing functions are:

  • 'easeInQuad'
  • 'easeOutQuad'
  • 'easeInOutQuad'
  • 'easeInCubic'
  • 'easeOutCubic'
  • 'easeInOutCubic'
  • 'easeInQuart'
  • 'easeOutQuart'
  • 'easeInOutQuart'
  • 'easeInQuint'
  • 'easeOutQuint'
  • 'easeInOutQuint'

Cheers.

ng2smoothscroll's People

Contributors

kavil avatar rudolfpleij avatar tdanielcox avatar

Stargazers

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

Watchers

 avatar

ng2smoothscroll's Issues

Wouldn't accept import statement.

Your documentation on the npm website states to use the import statement:
import { SmoothScrollToDirective, SmoothScrollDirective } from "ng2SmoothScroll";
however this was highlighted as a missing package. Used "ng2-smooth-scroll" and worked fine.

Scroll to element from another component

Hi!
I have some conflicts to do scroll to an element of a different component from where my button is.
For example mi button is in app-button-view and my div that I want to go is in app-home-view but in the end everything is shown on the same page.

My button is this
<button class="btn btn-enlace" scrollTo="sectionA" duration="1800"(click)="onClick()">Ver contabilidad</button>
My div is this
<div id="sectionA"></div>

How can I do the button see the id of my div?

Is this project still being supported?

Hello, I am very interested in this smooth scroll project for angular2+.
Is anyone still supporting this?

I am having trouble getting it to work... maybe due to poor documentation on the README.
Let me know if anyone can help me. Thanks!

Parameters not typesafe, fails to compile on my project

Please make types explicit because some projects require typesafe parameters. Thanks!

[0] ERROR in node_modules/ng2-smooth-scroll/lib/ng2-SmoothScroll.directive.ts(27,34): error TS7006: Parameter 'element' implicitly has an 'any' type.
[0] node_modules/ng2-smooth-scroll/lib/ng2-SmoothScroll.directive.ts(37,33): error TS7006: Parameter 'element' implicitly has an 'any' type.
[0] node_modules/ng2-smooth-scroll/lib/ng2-SmoothScroll.directive.ts(74,52): error TS7006: Parameter 'target' implicitly has an 'any' type.
[0] node_modules/ng2-smooth-scroll/lib/ng2-SmoothScroll.directive.ts(79,36): error TS7006: Parameter 'element' implicitly has an 'any' type.
[0] node_modules/ng2-smooth-scroll/lib/ng2-SmoothScroll.directive.ts(88,35): error TS7006: Parameter 'element' implicitly has an 'any' type.
[0] node_modules/ng2-smooth-scroll/lib/ng2-SmoothScroll.directive.ts(116,23): error TS7006: Parameter 'element' implicitly has an 'any' type.
[0] node_modules/ng2-smooth-scroll/lib/ng2-SmoothScroll.directive.ts(116,32): error TS7006: Parameter 'options' implicitly has an 'any' type.
[0] node_modules/ng2-smooth-scroll/lib/ng2-SmoothScroll.directive.ts(150,36): error TS7006: Parameter 'type' implicitly has an 'any' type.
[0] node_modules/ng2-smooth-scroll/lib/ng2-SmoothScroll.directive.ts(150,42): error TS7006: Parameter 'time' implicitly has an 'any' type.
[0] node_modules/ng2-smooth-scroll/lib/ng2-SmoothScroll.directive.ts(171,34): error TS7006: Parameter 'element' implicitly has an 'any' type.
[0] node_modules/ng2-smooth-scroll/lib/ng2-SmoothScroll.directive.ts(191,5): error TS7034: Variable 'position' implicitly has type 'any' in some locations where its type cannot be determined.
[0] node_modules/ng2-smooth-scroll/lib/ng2-SmoothScroll.directive.ts(210,7): error TS7005: Variable 'position' implicitly has an 'any' type.

getEndLocation - no container accounted for.

Hi there,

I found this library and it works great! However I noticed that when I had multiple points in a container div the getEndLocation function would act all wonky and not account for the container offset. I was able to resolve this issue by adding the following to the top of the function directly following the variable declarations.

if (containerPresent) {
    absoluteElementTop = elementRect.top + container.scrollTop;
}

Cheers.

href="#" Breaks SPA Concept?

Items that don't reference routerLink=['/some-link']" just call back to the server, thereby refreshing the page, which defeats the purpose of a Single Page Application.

// Basic
<a href="#"
scrollTo="my-element-3">
Click me!
</a>

Am I using this incorrectly?

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.