Skip to content

Commit 7507dd4

Browse files
committed
fix(ScrollCollapse): fixed issue where affix would not be calculated in nested element with position
1 parent e956457 commit 7507dd4

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

src/app/app.component.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
<h1>Scroll down ↓</h1>
2+
23
<nav class="nav" snScrollCollapse>
34
My nav&nbsp;
45
<span class="down">Going down</span>
56
<span class="up">Going up</span>
67
</nav>
8+
79
<div class="spacer"></div>
8-
<div class="bar" snScrollCollapse>
9-
Sticky when scrolled passed
10+
11+
<!-- nested element -->
12+
<div class="bar-container">
13+
<div class="bar" snScrollCollapse>
14+
Sticky when scrolled passed
15+
</div>
1016
</div>
17+
1118
<div class="spacer"></div>

src/app/app.component.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@
5656
.spacer {
5757
height: 150vh;
5858
}
59+
60+
.bar-container {
61+
position: relative;
62+
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class ScrollCollapseDirective implements AfterViewInit, OnDestroy {
5454
*
5555
* @memberof ScrollCollapseDirective
5656
*/
57-
public originalTop: number;
57+
public originalTop = 0;
5858
/**
5959
* Original offsetHeight of element
6060
*
@@ -130,7 +130,12 @@ export class ScrollCollapseDirective implements AfterViewInit, OnDestroy {
130130
*/
131131
public ngAfterViewInit(): void {
132132
const el: HTMLElement = this.el.nativeElement;
133-
this.originalTop = el.offsetTop;
133+
// Check if `getBoundingClientRect` is a function in case running
134+
// in an platform without the DOM
135+
if (typeof el.getBoundingClientRect === 'function') {
136+
const elBounds = el.getBoundingClientRect();
137+
this.originalTop = elBounds.top + this.windowRef.scrollY;
138+
}
134139
this.originalHeight = el.offsetHeight;
135140

136141
this.ngZone.runOutsideAngular(() => {

src/styles.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ h1 {
33
font-family: Arial, Helvetica, sans-serif;
44
font-size: 250%;
55
}
6+
7+
body {
8+
margin: 0;
9+
padding: 0;
10+
}

0 commit comments

Comments
 (0)