/
smartNav.js
164 lines (158 loc) · 5.23 KB
/
smartNav.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
/**
* Module that detects the last scroll direction and
* current scroll position to then add classes for
* when the user is scrolling up or down the page
* to show/hide the nav when scrolling in any particular
* direction. Also when the user has scrolled beyond the
* height of header adds a class to allow the nav to be
* minimized or hidden.
*
* When the user has scrolled down the page the class
* `scrolling-down` will be added, when scrolling up
* `scrolling-up`. If the user has scrolled beyond the
* height of the nav the class `minimised-mode`.
*
* If the element is at the top of the viewport or above
* it then the class `affix` will be added. This is useful
* for making an element sticky when user has scrolled to it
*
* @module sn.smartNav
* @main sn.smartNav
* @author SOON_
*/
angular.module('sn.smartNav', [
])
/**
* @example
* `<nav sn-smart-nav></nav>`
* @class snSmartNav
* @param {Service} $window : Angular.js wrapper for window Object
* @param {Service} $document : Angular.js wrapper for document Object
*/
.directive('snSmartNav',[
'$window',
'$document',
function ($window, $document){
return {
restrict: 'A',
link: function($scope, $element){
/**
* The last recorded scrollTop position
* @private
* @property lastScrollTop
* @type {Number}
*/
var lastScrollTop = 0;
/**
* True if the last scroll direction was down the page
* @private
* @property scrollingDown
* @type {Boolean}
*/
var scrollingDown = false;
/**
* @method getScrollTop
* @returns {Number} the number of pixel from top of
* page the user has scrolled
*/
var getScrollTop = function getScrollTop() {
var doc = $document[0].documentElement,
body = $document[0].body,
scrollTop = ( (doc && doc.scrollTop) || (body && body.scrollTop) || 0 );
return scrollTop;
};
/**
* The number of pixels from the top of the page
* @property {Number} positionFromTop
*/
var positionFromTop = (getScrollTop() + $element[0].getBoundingClientRect().top);
/**
* @method isScrollingDown
* @private
* @param {Number} currentScrollTop
* @return {Boolean} True if last scroll direction is down
*/
var isScrollingDown = function isScrollingDown(currentScrollTop){
return currentScrollTop > lastScrollTop;
};
/**
* @method isScrollingUp
* @private
* @param {Number} currentScrollTop
* @return {Boolean} True if last scroll direction is up
*/
var isScrollingUp = function isScrollingUp(currentScrollTop){
return currentScrollTop < lastScrollTop;
};
/**
* Calulate the current scroll direction and add relevent classes
* @private
* @method calScrollDir
* @param {Number} scrollTop
*/
var calScrollDir = function calScrollDir(scrollTop){
if ( scrollingDown && isScrollingUp(scrollTop) ) {
scrollingDown = false;
$element.removeClass('scrolling-down');
$element.addClass('scrolling-up');
} else if ( !scrollingDown && isScrollingDown(scrollTop) ){
scrollingDown = true;
$element.removeClass('scrolling-up');
$element.addClass('scrolling-down');
}
};
/**
* Calulate if the user has scrolled beyond the height of the element
* @private
* @method calMinimisedMode
* @param {Number} scrollTop
*/
var calMinimisedMode = function calMinimisedMode(scrollTop){
if (scrollTop > $element[0].offsetHeight) {
$element.addClass('minimised-mode');
} else {
$element.removeClass('minimised-mode');
}
};
/**
* Calulate if the element is at top or above viewport
* so we can 'affix' it to top of the viewport.
* @private
* @method calAffixedMode
*/
var calAffixedMode = function calAffixedMode(scrollTop){
if (scrollTop >= positionFromTop ) {
$element.addClass('affix');
} else {
$element.removeClass('affix');
}
};
/**
* window `scroll` event handler.
* Gets the current scroll postion and calulates
* scroll direction and whether to enable minimise mode
* @private
* @method onScroll
*/
var onScroll = function onScroll() {
var scrollTop = getScrollTop();
calScrollDir(scrollTop);
calMinimisedMode(scrollTop);
calAffixedMode(scrollTop);
lastScrollTop = scrollTop;
};
/**
* Clear event listeners
* @method onDestroy
*/
var onDestroy = function onDestroy(){
angular.element($window).off('scroll', onScroll);
};
$scope.$on('$destroy', onDestroy);
angular.element($window).on('scroll', onScroll);
onScroll();
}
};
}
]);