Skip to content

Commit ea747b3

Browse files
authored
Merge pull request #23 from thisissoon/feature/y-offset
Feature/y offset
2 parents dd94f37 + 6689981 commit ea747b3

File tree

7 files changed

+116
-17
lines changed

7 files changed

+116
-17
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ In this scenario the nav element will have the class `sn-affix` added when the u
104104
}
105105
```
106106

107+
A `[yOffset]` can also be applied. Here `sn-affix` will be added when the top of the viewport is within 200 pixels of the top of the nav.
108+
109+
```html
110+
<header>...</header>
111+
<nav class="foo" snScrollCollapse [yOffset]="200">
112+
...
113+
</nav>
114+
```
115+
107116
### Minimise mode
108117

109118
In this scenario the nav element will have the class `sn-minimise` added when the user scrolls 100px (the original height of the element) down the page.

e2e/app.e2e-spec.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,79 @@
11
import { AppPage } from './app.po';
22
import { browser, element, by } from 'protractor';
33

4-
describe('ScrollCollapse Lib E2E Tests', function () {
4+
describe('ScrollCollapse Lib E2E Tests', function() {
55
let page: AppPage;
66

7-
beforeEach(() => page = new AppPage());
7+
beforeEach(() => (page = new AppPage()));
88

99
beforeEach(() => page.navigateTo());
1010

1111
beforeEach(() => browser.executeScript('window.scrollTo(0,0)'));
1212

1313
afterEach(() => {
14-
browser.manage().logs().get('browser').then((browserLog: any[]) => {
15-
expect(browserLog).toEqual([]);
16-
});
14+
browser
15+
.manage()
16+
.logs()
17+
.get('browser')
18+
.then((browserLog: any[]) => {
19+
expect(browserLog).toEqual([]);
20+
});
1721
});
1822

1923
describe('scroll direction', () => {
2024
it('should add scrolling direction class', () => {
2125
page.scrollTo();
22-
expect(page.getNavElement().getAttribute('class')).not.toContain('sn-scrolling-down');
23-
expect(page.getNavElement().getAttribute('class')).not.toContain('sn-scrolling-up');
26+
expect(page.getNavElement().getAttribute('class')).not.toContain(
27+
'sn-scrolling-down'
28+
);
29+
expect(page.getNavElement().getAttribute('class')).not.toContain(
30+
'sn-scrolling-up'
31+
);
2432

2533
page.scrollTo(0, 10);
2634
page.scrollTo(0, 200);
27-
expect(page.getNavElement().getAttribute('class')).toContain('sn-scrolling-down');
28-
expect(page.getNavElement().getAttribute('class')).not.toContain('sn-scrolling-up');
29-
35+
expect(page.getNavElement().getAttribute('class')).toContain(
36+
'sn-scrolling-down'
37+
);
38+
expect(page.getNavElement().getAttribute('class')).not.toContain(
39+
'sn-scrolling-up'
40+
);
3041

3142
page.scrollTo(0, 100);
32-
expect(page.getNavElement().getAttribute('class')).not.toContain('sn-scrolling-down');
33-
expect(page.getNavElement().getAttribute('class')).toContain('sn-scrolling-up');
43+
expect(page.getNavElement().getAttribute('class')).not.toContain(
44+
'sn-scrolling-down'
45+
);
46+
expect(page.getNavElement().getAttribute('class')).toContain(
47+
'sn-scrolling-up'
48+
);
3449
});
3550
});
3651

3752
describe('minimise mode', () => {
3853
it('should add "sn-minimise" class', () => {
3954
page.scrollTo();
4055
page.scrollTo(0, 10);
41-
expect(page.getNavElement().getAttribute('class')).not.toContain('sn-minimise');
56+
expect(page.getNavElement().getAttribute('class')).not.toContain(
57+
'sn-minimise'
58+
);
4259

4360
page.scrollTo(0, 110);
44-
expect(page.getNavElement().getAttribute('class')).toContain('sn-minimise');
61+
expect(page.getNavElement().getAttribute('class')).toContain(
62+
'sn-minimise'
63+
);
4564
});
4665
});
4766

4867
describe('affix mode', () => {
4968
it('should add "sn-affix" class', () => {
5069
page.scrollTo();
5170
page.scrollTo(0, 10);
52-
expect(page.getBarElement().getAttribute('class')).not.toContain('sn-affix');
71+
expect(page.getBarElement().getAttribute('class')).not.toContain(
72+
'sn-affix'
73+
);
5374

5475
page.scrollTo(0, 768 * 3);
5576
expect(page.getBarElement().getAttribute('class')).toContain('sn-affix');
5677
});
5778
});
58-
5979
});

e2e/app.po.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ export class AppPage {
1717
getBarElement() {
1818
return element(by.css('.bar'));
1919
}
20+
21+
getOffsetBarElement() {
22+
return element(by.css('.bar--offset'));
23+
}
2024
}

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.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,42 @@ describe('ScrollCollapseDirective', () => {
199199
});
200200
expect(directive.affixMode).toBeFalsy();
201201
});
202+
203+
it('should factor in yOffset property when calculating affix mode', () => {
204+
directive.originalHeight = 100;
205+
directive.originalTop = 500;
206+
directive.yOffset = 200;
207+
directive.calculateAffixMode({
208+
scrollX: 0,
209+
scrollY: 0,
210+
width: 1366,
211+
height: 768
212+
});
213+
expect(directive.affixMode).toBeFalsy();
214+
215+
directive.calculateAffixMode({
216+
scrollX: 0,
217+
scrollY: 200,
218+
width: 1366,
219+
height: 768
220+
});
221+
expect(directive.affixMode).toBeFalsy();
222+
223+
directive.calculateAffixMode({
224+
scrollX: 0,
225+
scrollY: 300,
226+
width: 1366,
227+
height: 768
228+
});
229+
expect(directive.affixMode).toBeTruthy();
230+
231+
directive.calculateAffixMode({
232+
scrollX: 0,
233+
scrollY: 299,
234+
width: 1366,
235+
height: 768
236+
});
237+
expect(directive.affixMode).toBeFalsy();
238+
});
202239
});
203240
});

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

Lines changed: 12 additions & 1 deletion
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 sn-affix class 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
*
@@ -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)