Skip to content

Commit 1336f0b

Browse files
feat(scroll directive): add yOffset input to class setting functions
apply classes on scroll taking into account yOffset property
1 parent dd94f37 commit 1336f0b

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/app/app.component.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ <h1>Scroll down ↓</h1>
1616
</div>
1717

1818
<div class="spacer"></div>
19+
20+
<!-- nested element -->
21+
<div class="bar-container">
22+
<div class="bar bar--offset" [yOffset]="200" snScrollCollapse>
23+
Classes applied when original Y position of element approaches yOffset. [yOffset]="200"
24+
</div>
25+
</div>
26+
27+
<div class="spacer"></div>

src/app/app.component.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@
5353
z-index: 9999;
5454
}
5555

56+
.bar.bar--offset.sn-affix {
57+
top: 100px;
58+
}
59+
60+
.bar.bar--offset.sn-minimise {
61+
background-color: #586e5d;
62+
height: 50px;
63+
}
64+
5665
.spacer {
5766
height: 150vh;
5867
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ export class ScrollCollapseDirective implements AfterViewInit, OnDestroy {
7474
* @memberof ScrollCollapseDirective
7575
*/
7676
@Input() public debounce = 0;
77+
/**
78+
* Number of pixels before the elements originalTop
79+
* position is scroll to that the classes will be applied.
80+
* This value will need to take into account elements which become
81+
* fixed above this element while scrolling as they reduce
82+
* the height of the document and the scrollY number.
83+
*
84+
* @default 0
85+
* @memberof ScrollCollapseDirective
86+
*/
87+
@Input() public yOffset = 0;
7788
/**
7889
* Returns true if last scroll direction is UP
7990
*
@@ -190,7 +201,7 @@ export class ScrollCollapseDirective implements AfterViewInit, OnDestroy {
190201
*/
191202
public calculateMinimiseMode(viewport: Viewport): void {
192203
this.minimiseMode =
193-
viewport.scrollY >= this.originalHeight + this.originalTop;
204+
viewport.scrollY >= this.originalHeight + this.originalTop - this.yOffset;
194205
}
195206
/**
196207
* Calculate if the user has scrolled pass the origin height of
@@ -199,7 +210,7 @@ export class ScrollCollapseDirective implements AfterViewInit, OnDestroy {
199210
* @memberof ScrollCollapseDirective
200211
*/
201212
public calculateAffixMode(viewport: Viewport): void {
202-
this.affixMode = viewport.scrollY >= this.originalTop;
213+
this.affixMode = viewport.scrollY >= this.originalTop - this.yOffset;
203214
}
204215
/**
205216
* Return current viewport values

0 commit comments

Comments
 (0)