Skip to content

Commit

Permalink
fix(ScrollCollapse): fixed issue where affix would not be calculated …
Browse files Browse the repository at this point in the history
…in nested element with position
  • Loading branch information
edoparearyee committed Feb 19, 2018
1 parent e956457 commit 7507dd4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<h1>Scroll down ↓</h1>

<nav class="nav" snScrollCollapse>
My nav&nbsp;
<span class="down">Going down</span>
<span class="up">Going up</span>
</nav>

<div class="spacer"></div>
<div class="bar" snScrollCollapse>
Sticky when scrolled passed

<!-- nested element -->
<div class="bar-container">
<div class="bar" snScrollCollapse>
Sticky when scrolled passed
</div>
</div>

<div class="spacer"></div>
4 changes: 4 additions & 0 deletions src/app/app.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@
.spacer {
height: 150vh;
}

.bar-container {
position: relative;
}
9 changes: 7 additions & 2 deletions src/app/scroll-collapse/scroll-collapse.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class ScrollCollapseDirective implements AfterViewInit, OnDestroy {
*
* @memberof ScrollCollapseDirective
*/
public originalTop: number;
public originalTop = 0;
/**
* Original offsetHeight of element
*
Expand Down Expand Up @@ -130,7 +130,12 @@ export class ScrollCollapseDirective implements AfterViewInit, OnDestroy {
*/
public ngAfterViewInit(): void {
const el: HTMLElement = this.el.nativeElement;
this.originalTop = el.offsetTop;
// Check if `getBoundingClientRect` is a function in case running
// in an platform without the DOM
if (typeof el.getBoundingClientRect === 'function') {
const elBounds = el.getBoundingClientRect();
this.originalTop = elBounds.top + this.windowRef.scrollY;
}
this.originalHeight = el.offsetHeight;

this.ngZone.runOutsideAngular(() => {
Expand Down
5 changes: 5 additions & 0 deletions src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ h1 {
font-family: Arial, Helvetica, sans-serif;
font-size: 250%;
}

body {
margin: 0;
padding: 0;
}

0 comments on commit 7507dd4

Please sign in to comment.