-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsticky-header-knockout.coffee
278 lines (220 loc) · 8.21 KB
/
sticky-header-knockout.coffee
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
###
Sticky Header Knockout
======================
- Stick a header to the screen when scrolled into view
- Headers knockout and replace the sticky header as the container scrolls
Dependencies
------------
- jQuery
Example
=======
Javascript
----------
```
stickyHeaderKnockout = new HelperStickyHeaderKnockout
$headers: $('.header')
$stickyHeader: $('.sticky-header')
```
CSS
---
```
.sticky-header {
left: 0;
position: fixed;
right: 0;
top: 0;
}
```
Options
=======
- $headers
- Mandatory
- jQuery object, eg: `$('.header')`
- Header elements will become sticky when scrolled into view
- $stickyHeader
- Mandatory
- jQuery object, eg: `$('.sticky-header')`
- Header currently sticky (if any) will be embedded within this element
- $container
- Optional
- Default: `$(window)`
- jQuery object of scrollable container
- padding
- Optional
- Default: 0
- Add padding to the scroll measurement code, for headers that aren't stuck to the top
- classStickyHeaderIsVisible
- Optional
- Default: null
- Add a class to the sticky header when visible
- classStickyHeaderIsNudging
- Optional
- Default: null
- Add a class to the sticky header when nudged
- classHeaderIsNudging
- Optional
- Default: null
- Add a class to the header when nudging the sticky header
- fastScrollSpeed
- Optional
- Default: 50
- Some calculations are skipped to avoid overload if the container scrolls faster than this speed
###
define(
[
'jquery'
],
(
$
) ->
class HelperStickyHeaderKnockout
DEFAULT_OPTIONS = {
$headers: null
$stickyHeader: null
$container: $(window)
padding: 0
classStickyHeaderIsVisible: null
classStickyHeaderIsNudging: null
classHeaderIsNudging: null
fastScrollSpeed: 50
}
constructor: (options = {}) ->
@options = @_processOptions(options)
@headers = @_processHeaders()
@headersReversed = @headers.slice(0).reverse()
@_setupStickyHeaders()
@_setupEvents()
@_update()
_calculateContainerScrollTop: ->
return @options.containerElement.scrollTop + @options.padding
_calculateStickyHeader: (scrollTop) ->
for header in @headersReversed
if scrollTop >= header.positionTop
return header
return null
_calculateStickyHeaderClassList: (stickyHeader, stickyHeaderNudge) ->
classList = []
if stickyHeader? and @options.classStickyHeaderIsVisible?
classList.push(@options.classStickyHeaderIsVisible)
if stickyHeaderNudge? and @options.classStickyHeaderIsNudging?
classList.push(@options.classStickyHeaderIsNudging)
return classList
_calculateStickyHeaderNudge: (scrollTop) ->
for header in @headersReversed
scrollTopMin = header.positionTop - header.height
scrollTopMax = header.positionTop
if scrollTop > scrollTopMin and scrollTop < scrollTopMax
return header
return null
_calculateStickyHeaderPosition: (scrollTop, stickyHeaderNudge) ->
if not stickyHeaderNudge?
return 0
return ((scrollTop - stickyHeaderNudge.positionTop + stickyHeaderNudge.height) * -1)
_eventContainerScroll: =>
@_update()
_isScrollingFast: (scrollTopNow, scrollTopPrevious) ->
scrollSpeed = Math.abs(scrollTopNow - scrollTopPrevious)
return scrollSpeed > @options.fastScrollSpeed
_processHeaders: ->
headers = []
for headerElement in @options.$headers
header = {}
header.element = headerElement
header.elementSticky = headerElement.cloneNode(true)
header.positionTop = headerElement.offsetTop
header.height = headerElement.offsetHeight
headers.push(header)
return headers
_processOptions: (options) ->
options = $.extend(true, {}, DEFAULT_OPTIONS, options)
@_validateOptions(options)
options.headerElements = options.$headers.get(0)
options.stickyHeaderElement = options.$stickyHeader.get(0)
options.containerElement = options.$container.get(0)
return options
_resetStickyHeaderClassList: ->
classList = @options.stickyHeaderElement.classList
if @options.classStickyHeaderIsVisible?
classList.remove(@options.classStickyHeaderIsVisible)
if @options.classStickyHeaderIsNudging?
classList.remove(@options.classStickyHeaderIsNudging)
_setStickyHeader: (header) ->
return true if header is @stickyHeader
stickyHeaderPrevious = @stickyHeader
@stickyHeader = header
if @stickyHeader?
@stickyHeader.elementSticky.style.display = ''
@options.stickyHeaderElement.style.height = @stickyHeader.height + 'px'
@options.stickyHeaderElement.style.overflow = 'hidden'
else
@options.stickyHeaderElement.style.height = ''
@options.stickyHeaderElement.style.overflow = ''
if stickyHeaderPrevious?
stickyHeaderPrevious.elementSticky.style.display = 'none'
_setStickyHeaderClassList: (classList) ->
classString = classList.join(' ')
return true if classString is @stickyHeaderClassString
@stickyHeaderClassString = classString
@_resetStickyHeaderClassList()
for classItem in classList
@options.stickyHeaderElement.classList.add(classItem)
_setStickyHeaderNudge: (stickyHeaderNudge) ->
return true if stickyHeaderNudge is @stickyHeaderNudge
if @stickyHeaderNudge? and @options.classHeaderIsNudging?
@stickyHeaderNudge.element.classList.remove(@options.classHeaderIsNudging)
@stickyHeaderNudge = stickyHeaderNudge
if stickyHeaderNudge? and @options.classHeaderIsNudging?
stickyHeaderNudge.element.classList.add(@options.classHeaderIsNudging)
_setStickyHeaderPosition: (position) ->
return true if position is @stickyHeaderPosition
@stickyHeaderPosition = position
@_setElementTranslateY(@options.stickyHeaderElement, position)
_setElementTranslateY: (element, translateY) ->
transformValue = "translate3d(0px, #{translateY}px, 0px)"
element.style["-webkit-transform"] = transformValue
element.style["-moz-transform"] = transformValue
element.style["-ms-transform"] = transformValue
element.style["-o-transform"] = transformValue
element.style["transform"] = transformValue
_setupEvents: ->
@options.$container
.on('scroll', @_eventContainerScroll)
.trigger('scroll')
_setupStickyHeaders: ->
for header in @headers
header.elementSticky.style.display = 'none'
@options.stickyHeaderElement.appendChild(header.elementSticky)
_update: (isForced = false) ->
scrollTopPrevious = @scrollTop
scrollTopNow = @_calculateContainerScrollTop()
@scrollTop = scrollTopNow
stickyHeader = @_calculateStickyHeader(scrollTopNow)
@_setStickyHeader(stickyHeader)
isScrollingFast = @_isScrollingFast(scrollTopNow, scrollTopPrevious)
if isScrollingFast and not isForced
stickyHeaderNudge = null
stickyHeaderPosition = 0
else
stickyHeaderNudge = @_calculateStickyHeaderNudge(scrollTopNow)
stickyHeaderPosition = @_calculateStickyHeaderPosition(scrollTopNow, stickyHeaderNudge)
@_setStickyHeaderPosition(stickyHeaderPosition)
@_setStickyHeaderNudge(stickyHeaderNudge)
stickyHeaderClassList = @_calculateStickyHeaderClassList(stickyHeader, stickyHeaderNudge)
@_setStickyHeaderClassList(stickyHeaderClassList)
if not isForced
@_updateFinal()
_updateFinal: ->
if @updateFinalTimeout?
clearTimeout(@updateFinalTimeout)
delete @updateFinalTimeout
@updateFinalTimeout = setTimeout(
=> @_update(true)
@options.fastScrollSpeed
)
_validateOptions: (options) ->
if not options.$stickyHeader?
throw new Error('Missing "$stickyHeader" option')
if not options.$headers?
throw new Error('Missing "$headers" option')
return HelperStickyHeaderKnockout
)