Skip to content

Commit 5d33ca7

Browse files
committed
feat(minimise): implemented detection for scrolling past element height assuming element is fixed at
1 parent 4856c3c commit 5d33ca7

4 files changed

Lines changed: 55 additions & 15 deletions

File tree

src/demo/app/app.component.css

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,32 @@
55
}
66

77
.foo {
8+
align-items: center;
89
background-color: #563d7c;
9-
left: 0;
10+
color: #ffffff;
11+
display: flex;
1012
height: 100px;
13+
justify-content: center;
14+
left: 0;
1115
position: fixed;
1216
right: 0;
17+
text-align: center;
1318
top: 0;
14-
transition: opacity .35s ease-in-out;
19+
transition: all .35s ease-in-out;
20+
}
21+
.foo .down,
22+
.foo .up {
23+
display: none;
1524
}
1625

17-
.foo.sn-scrolling-down {
18-
opacity: 0;
26+
.foo.sn-scrolling-down .down,
27+
.foo.sn-scrolling-up .up {
28+
display: block;
1929
}
2030

21-
.foo.sn-scrolling-up {
22-
opacity: 1;
31+
.foo.sn-minimise {
32+
background-color: #007bff;
33+
height: 50px;
2334
}
2435

2536
.spacer {

src/demo/app/app.component.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
<h1>Scroll down ↓ and right →</h1>
2-
<nav class="foo" scrollCollapse [debounce]="0"></nav>
1+
<h1>Scroll down ↓</h1>
2+
<nav class="foo" scrollCollapse [debounce]="0">
3+
My nav&nbsp;
4+
<span class="down">Going down</span>
5+
<span class="up">Going up</span>
6+
</nav>
37
<div class="spacer"></div>

src/lib/src/classes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export const directionUpClass = 'class.sn-scrolling-up';
22
export const directionDownClass = 'class.sn-scrolling-down';
3+
export const affixClass = 'class.sn-affix';
4+
export const minimiseClass = 'class.sn-minimise';

src/lib/src/scroll-collapse.directive.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import * as classes from './classes';
1717
* that detects scroll direction and adds a `sn-scrolling-up` or
1818
* `sn-scrolling-down` class to the element. The library can also detect when
1919
* the user has scrolled passed the element and apply a `sn-affix` class.
20-
* Useful for make a element sticky when the user has scrolled beyond it. This
21-
* library can will also apply `sn-minimize` class after the user has scrolled
22-
* beyond the height of the element.
20+
* Useful for make a element sticky when the user has scrolled beyond it.
21+
* This library can will also apply `sn-minimize` class after the user has
22+
* scrolled beyond the height of the element.
2323
*
2424
* @example
2525
* ```
@@ -89,6 +89,15 @@ export class ScrollCollapseDirective implements OnInit, OnDestroy {
8989
public get isScrollingDown(): boolean {
9090
return this.scrollDirection === Direction.DOWN;
9191
}
92+
/**
93+
* Returns true if the user has scrolled pass the origin height of
94+
* the element assuming the element is fixed at the top of the page
95+
*
96+
* @type {boolean}
97+
* @memberof ScrollCollapseDirective
98+
*/
99+
@HostBinding(classes.minimiseClass)
100+
public minimiseMode = false;
92101
/**
93102
* Creates an instance of ScrollCollapseDirective.
94103
* @param {ElementRef} el
@@ -106,7 +115,10 @@ export class ScrollCollapseDirective implements OnInit, OnDestroy {
106115
.takeUntil(this.ngUnsubscribe$)
107116
.debounceTime(this.debounce)
108117
.bufferCount(2, 1)
109-
.subscribe((events: Viewport[]) => this.calculateScrollDirection(events));
118+
.subscribe((events: Viewport[]) => {
119+
this.calculateScrollDirection(events);
120+
this.calculateMinimiseMode(events[1]);
121+
});
110122
}
111123
/**
112124
* On window scroll/resize/load events
@@ -131,10 +143,10 @@ export class ScrollCollapseDirective implements OnInit, OnDestroy {
131143
this.viewport$.next(viewport);
132144
}
133145
/**
134-
* Calculate scrollCollapse status and emit event
135-
* when viewport status has changed
146+
* Calculate last scroll direction by comparing y scroll position
147+
* of last two values of `viewport$` observable
136148
*
137-
* @param {Viewport} viewport
149+
* @param {Viewport[]} events
138150
* @memberof ScrollCollapseDirective
139151
*/
140152
public calculateScrollDirection(events: Viewport[]): void {
@@ -143,6 +155,17 @@ export class ScrollCollapseDirective implements OnInit, OnDestroy {
143155
this.scrollDirection = (pastEvent.scrollY > currentEvent.scrollY) ?
144156
Direction.UP : Direction.DOWN;
145157
}
158+
/**
159+
* Calculate if the user has scrolled pass the origin height of
160+
* the element assuming the element is fixed at the top of the page
161+
*
162+
* @param {Viewport} viewport
163+
* @memberof ScrollCollapseDirective
164+
*/
165+
public calculateMinimiseMode(viewport: Viewport): void {
166+
const el: HTMLElement = this.el.nativeElement;
167+
this.minimiseMode = viewport.scrollY > el.offsetHeight;
168+
}
146169
/**
147170
* trigger `ngUnsubscribe` complete on
148171
* component destroy lifecycle hook

0 commit comments

Comments
 (0)