Skip to content

Commit

Permalink
fix(module): compare position value to mapped offset value (#64)
Browse files Browse the repository at this point in the history
Position value was never compared against the mapped offset value. Instead, it was being compared against the initial offset value (if any). Therfore, mapped values never closed the animation loop as they never got satisfied by the animation stop method.

This fixes #62
  • Loading branch information
Nicky Lenaers authored Feb 2, 2018
1 parent f583b9f commit a2f4792
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/app/+home/home.component.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<button
(click)="show()"
[ngx-scroll-to-duration]="4000"
[ngx-scroll-to-duration]="1500"
[ngx-scroll-to]="'destination'"
[ngx-scroll-to-offset]="-10"
[ngx-scroll-to-offset-map]="offsetMap"
[ngx-scroll-to-easing]="'easeInOutQuint'"
>Go</button>
<section *ngIf="visible" id="destination">
You've reached your destination.
Expand Down
4 changes: 4 additions & 0 deletions src/app/+home/home.component.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
button {
margin-bottom: 100vh;
}

#destination {
margin-bottom: 200vh;
}
7 changes: 6 additions & 1 deletion src/app/+home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import { Component, OnInit } from '@angular/core';
export class HomeComponent implements OnInit {

public visible = false;
public offsetMap;

constructor() { }
constructor() {
this.offsetMap = new Map();
this.offsetMap
.set(600, -500);
}

ngOnInit() {
}
Expand Down
16 changes: 11 additions & 5 deletions src/app/modules/scroll-to/statics/scroll-to-animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export class ScrollToAnimation {
*/
private _windowScrollTop: number;

/**
* Mapped Offset taken from the active Offset Map.
*/
private _mappedOffset: number;

/**
* Class Constructor.
*
Expand Down Expand Up @@ -90,17 +95,17 @@ export class ScrollToAnimation {
const directionalDistance = this._startPosition - this._to;
this._distance = Math.abs(this._startPosition - this._to);

let offset = this._options.offset;
this._mappedOffset = this._options.offset;

// Set offset from Offset Map
if (this._isBrowser) {

this._options
.offsetMap
.forEach((value, key) => offset = window.innerWidth > key ? value : offset);
.forEach((value, key) => this._mappedOffset = window.innerWidth > key ? value : this._mappedOffset);
}

this._distance += offset * (directionalDistance <= 0 ? 1 : -1);
this._distance += this._mappedOffset * (directionalDistance <= 0 ? 1 : -1);
this._source$ = new ReplaySubject();
}

Expand All @@ -121,6 +126,7 @@ export class ScrollToAnimation {
* @returns Void
*/
private _loop = (): void => {

this._timeLapsed += this._tick;
this._percentage = (this._timeLapsed / this._options.duration);
this._percentage = (this._percentage > 1) ? 1 : this._percentage;
Expand All @@ -147,8 +153,8 @@ export class ScrollToAnimation {
const currPosition = this._isWindow ? this._windowScrollTop : this._container.scrollTop;

if (force
|| Math.trunc(this._position) === (this._to + this._options.offset)
|| currPosition === (this._to + this._options.offset)) {
|| Math.trunc(this._position) === (this._to + this._mappedOffset)
|| currPosition === (this._to + this._mappedOffset)) {
clearInterval(this._interval);
this._interval = null;
this._source$.complete();
Expand Down

0 comments on commit a2f4792

Please sign in to comment.