Angular 4 - Scroll Animation

Angular 4 - Scroll Animation

This one is fun. The solution, as with most things angular 2, is observables.

  getTargetElementRef(currentYPos: int): ElementRef {
// you need to figure out how this works
// I can't comment much on it without knowing more about the page
// but you inject the host ElementRef in the component / directive constructor and use normal vanillaJS functions to find other elements
}
//capture the scroll event and pass to a function that triggers your own event for clarity and so you can manually trigger
scrollToSource: Subject<int> = new Subject<int>();
@HostListener("window:scroll", ['$event'])
onWindowScroll($event: any): void {
var target = getTargetElementRef(window.pageYOffset);
this.scrollTo(target);
}

scrollTo(target: ElementRef): void {
// this assumes you're passing in an ElementRef, it may or may not be appropriate, you can pass them to functions in templates with template variable syntax such as: <div #targetDiv>Scroll Target</div> <button (click)="scrollTo(targetDiv)">Click To Scroll</button>
this.scrollToSource.next(target.nativeElement.offsetTop);
}

//switch map takes the last value emitted by an observable sequence, in this case, the user's latest scroll position, and transforms it into a new observable stream
this.scrollToSource.switchMap(targetYPos => {
return Observable.interval(100) //interval just creates an observable stream corresponding to time, this emits every 1/10th of a second. This can be fixed or make it dynamic depending on the distance to scroll
.scan((acc, curr) => acc + 5, window.pageYOffset) // scan takes all values from an emitted observable stream and accumulates them, here you're taking the current position, adding a scroll step (fixed at 5, though this could also be dynamic), and then so on, its like a for loop with +=, but you emit every value to the next operator which scrolls, the second argument is the start position
.do(position => window.scrollTo(0, position)) /// here is where you scroll with the results from scan
.takeWhile(val => val < targetYPos); // stop when you get to the target
}).subscribe(); //don't forget!

With a click this is easy to use. You just bind scrollTo to a click

This only works for scrolling in one direction, However this should get you started. You can make scan smarter so it subtracts if you need to go up, and instead use a function inside takeWhile that figures out the correct termination condition based on if going up or down.

edit: rxjs 5+ compatible version

  this.scrollToSource.pipe(switchMap(targetYPos => 
interval(100).pipe( //interval just creates an observable stream corresponding to time, this emits every 1/10th of a second. This can be fixed or make it dynamic depending on the distance to scroll
scan((acc, curr) => acc + 5, window.pageYOffset), // scan takes all values from an emitted observable stream and accumulates them, here you're taking the current position, adding a scroll step (fixed at 5, though this could also be dynamic), and then so on, its like a for loop with +=, but you emit every value to the next operator which scrolls, the second argument is the start position
takeWhile(val => val < targetYPos)) // stop when you get to the target
)).subscribe(position => window.scrollTo(0, position)); // here is where you scroll with the results from scan

Scroll down div animation in Angular

This works by specifically adding a window scroll event listener.

import { Component, VERSION, HostListener } from "@angular/core";

@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
name = "Angular " + VERSION.major;
constructor(){
window.addEventListener("scroll", (event)=>{
debugger;
const box = document.querySelector('.box');
if (window.pageYOffset < box.clientHeight ) {
box.classList.add('colorChange');
} else {
box.classList.remove('colorChange');
}
});
}
}

Here's link to working example

Scroll div on Mouseover with Angular 4/6 Animation

Some of the steps are pretty basic Angular. I'm sure the community would appreciate it if you showed that you put some effort into finding the solution yourself. specifically, parts 1 and 3 are pretty basic. I'll give you the benefit of the doubt and assume that you gave it an effort and are still stumped :-)

  1. instead of onmouseover= and onmouseout=, you will use (mouseover)= and (mouseout)=, putting a function from the component's class after the =. Learn more about event binding
  2. instead of passing an ID, in Angular you can use a template reference variable. In the element tag that you want a reference to, add #someElementName, and then when you call the function on a mouse event, pass someElementName. Learn more about template reference variables
  3. timer1 will become a class member of the component (this.timer1 = setTimout...). Learn more about component basics

a stackblitz with the result

Angular 4 - how to trigger an animation when a div comes into the viewport?

I've created a directive that emits an event as soon as the element is either completely within view or it's upper edge has reached view's upper edge.

Here's a plunker: https://embed.plnkr.co/mlez1dXjR87FNBHXq1YM/

It's used like this:

<div (appear)="onAppear()">...</div>

Here's the directive:

import {
ElementRef, Output, Directive, AfterViewInit, OnDestroy, EventEmitter
} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Subscription} from 'rxjs/Subscription';
import 'rxjs/add/observable/fromEvent';
import 'rxjs/add/operator/startWith';

@Directive({
selector: '[appear]'
})
export class AppearDirective implements AfterViewInit, OnDestroy {
@Output()
appear: EventEmitter<void>;

elementPos: number;
elementHeight: number;

scrollPos: number;
windowHeight: number;

subscriptionScroll: Subscription;
subscriptionResize: Subscription;

constructor(private element: ElementRef){
this.appear = new EventEmitter<void>();
}

saveDimensions() {
this.elementPos = this.getOffsetTop(this.element.nativeElement);
this.elementHeight = this.element.nativeElement.offsetHeight;
this.windowHeight = window.innerHeight;
}
saveScrollPos() {
this.scrollPos = window.scrollY;
}
getOffsetTop(element: any){
let offsetTop = element.offsetTop || 0;
if(element.offsetParent){
offsetTop += this.getOffsetTop(element.offsetParent);
}
return offsetTop;
}
checkVisibility(){
if(this.isVisible()){
// double check dimensions (due to async loaded contents, e.g. images)
this.saveDimensions();
if(this.isVisible()){
this.unsubscribe();
this.appear.emit();
}
}
}
isVisible(){
return this.scrollPos >= this.elementPos || (this.scrollPos + this.windowHeight) >= (this.elementPos + this.elementHeight);
}

subscribe(){
this.subscriptionScroll = Observable.fromEvent(window, 'scroll').startWith(null)
.subscribe(() => {
this.saveScrollPos();
this.checkVisibility();
});
this.subscriptionResize = Observable.fromEvent(window, 'resize').startWith(null)
.subscribe(() => {
this.saveDimensions();
this.checkVisibility();
});
}
unsubscribe(){
if(this.subscriptionScroll){
this.subscriptionScroll.unsubscribe();
}
if(this.subscriptionResize){
this.subscriptionResize.unsubscribe();
}
}

ngAfterViewInit(){
this.subscribe();
}
ngOnDestroy(){
this.unsubscribe();
}
}

How to animate ScrollTop with @angular/animations?

I managed to create this, using three Angular animations states : small, big and normal, corresponding to the height of the div :

animations.ts

Here, I used one state variable per div as an example, and I set the each of these states to normal by default.
Then, depending on which div I click on, I toggle the states according to what we want to happen : making the div we click on bigger and the others smaller

export const expand = [
trigger('expand', [
state('big', style({
'height': '200px'
})),
state('normal', style({
'height': '100px'
})),
state('small', style({
'height': '50px'
})),
transition('* => *', [group([
animate(1000)
]
)])
]),
]

app.component.ts

import { expand } from './animations';

@Component({
...
animations: [expand]
})
export class AppComponent implements OnInit {
expandState1 = 'normal';
expandState2 = 'normal';
expandState3 = 'normal';
expandState4 = 'normal';
expandState5 = 'normal';

ngOnInit() {
this.resetStates();
}

resetStates() {
this.expandState1 = 'normal';
this.expandState2 = 'normal';
this.expandState3 = 'normal';
this.expandState4 = 'normal';
this.expandState5 = 'normal';
}

toggleShowDiv(divName: string) {
if (divName === 'div1') {
if (this.expandState1 === 'normal' || this.expandState1 === 'small') {
this.setToBig([1]);
this.setToSmall([2, 3, 4, 5]);
} else if (this.expandState1 === 'big' || this.expandState1 === 'small') {
this.resetStates();
}
} else if (divName === 'div2') {
if (this.expandState2 === 'normal' || this.expandState2 === 'small') {
this.setToBig([2]);
this.setToSmall([1, 3, 4, 5]);
} else if (this.expandState2 === 'big') {
this.resetStates();
}
} else if (divName === 'div3') {
if (this.expandState3 === 'normal' || this.expandState3 === 'small') {
this.setToBig([3]);
this.setToSmall([1, 2, 4, 5]);
} else if (this.expandState3 === 'big') {
this.resetStates();
}
} else if (divName === 'div4') {
if (this.expandState4 === 'normal' || this.expandState4 === 'small') {
this.setToBig([4]);
this.setToSmall([1, 2, 3, 5]);
} else if (this.expandState4 === 'big') {
this.resetStates();
}
} else if (divName === 'div5') {
if (this.expandState5 === 'normal' || this.expandState5 === 'small') {
this.setToBig([5]);
this.setToSmall([1, 2, 3, 4]);
} else if (this.expandState5 === 'big') {
this.resetStates();
}
}
}

setToSmall(choices: any) {
for (let i = 0; i < choices.length; i++) {
switch (choices[i]) {
case 1:
this.expandState1 = 'small';
break;
case 2:
this.expandState2 = 'small';
break;
case 3:
this.expandState3 = 'small';
break;
case 4:
this.expandState4 = 'small';
break;
case 5:
this.expandState5 = 'small';
break;
default:
break;
}
}
}

setToBig(choices: any) {
for (let i = 0; i < choices.length; i++) {
switch (choices[i]) {
case 1:
this.expandState1 = 'big';
break;
case 2:
this.expandState2 = 'big';
break;
case 3:
this.expandState3 = 'big';
break;
case 4:
this.expandState4 = 'big';
break;
case 5:
this.expandState5 = 'big';
break;
default:
break;
}
}
}
}

And here is the corresponding template :

Each div has the reference to the animation trigger [@expand] and its state.

<div class="wrapper scrollableDiv">
<div [@expand]="expandState1" (click)="toggleShowDiv('div1')" class="content divA">THIS IS CONTENT DIV 1</div>
<div [@expand]="expandState2" (click)="toggleShowDiv('div2')" class="content divA">THIS IS CONTENT DIV 2</div>
<div [@expand]="expandState3" (click)="toggleShowDiv('div3')" class="content divA">THIS IS CONTENT DIV 3</div>
<div [@expand]="expandState4" (click)="toggleShowDiv('div4')" class="content divA">THIS IS CONTENT DIV 4</div>
<div [@expand]="expandState5" (click)="toggleShowDiv('div5')" class="content divA">THIS IS CONTENT DIV 5</div>
</div>

Here is a StackBlitz example I made for this : https://stackblitz.com/edit/angular-t47iyy

How to implement Scroll animation in Angular 8 with rxjs 6?

I prefer to use angular animation but for now I'm using jquery solution with
the following:

$("html, body").animate({ scrollTop: 0 }, "slow");

I found the following solution but it's not valid rxjs 6 code:
Angular 4 - Scroll Animation



Related Topics



Leave a reply



Submit