1 | 'use strict';
|
2 |
|
3 | import $ from 'jquery';
|
4 | import { GetYoDigits } from './foundation.util.core';
|
5 | import { MediaQuery } from './foundation.util.mediaQuery';
|
6 | import { Plugin } from './foundation.plugin';
|
7 | import { Triggers } from './foundation.util.triggers';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | class Sticky extends Plugin {
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | _setup(element, options) {
|
25 | this.$element = element;
|
26 | this.options = $.extend({}, Sticky.defaults, this.$element.data(), options);
|
27 | this.className = 'Sticky';
|
28 |
|
29 |
|
30 | Triggers.init($);
|
31 |
|
32 | this._init();
|
33 | }
|
34 |
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 | _init() {
|
41 | MediaQuery._init();
|
42 |
|
43 | var $parent = this.$element.parent('[data-sticky-container]'),
|
44 | id = this.$element[0].id || GetYoDigits(6, 'sticky'),
|
45 | _this = this;
|
46 |
|
47 | if($parent.length){
|
48 | this.$container = $parent;
|
49 | } else {
|
50 | this.wasWrapped = true;
|
51 | this.$element.wrap(this.options.container);
|
52 | this.$container = this.$element.parent();
|
53 | }
|
54 | this.$container.addClass(this.options.containerClass);
|
55 |
|
56 | this.$element.addClass(this.options.stickyClass).attr({ 'data-resize': id, 'data-mutate': id });
|
57 | if (this.options.anchor !== '') {
|
58 | $('#' + _this.options.anchor).attr({ 'data-mutate': id });
|
59 | }
|
60 |
|
61 | this.scrollCount = this.options.checkEvery;
|
62 | this.isStuck = false;
|
63 | $(window).one('load.zf.sticky', function(){
|
64 |
|
65 | _this.containerHeight = _this.$element.css("display") == "none" ? 0 : _this.$element[0].getBoundingClientRect().height;
|
66 | _this.$container.css('height', _this.containerHeight);
|
67 | _this.elemHeight = _this.containerHeight;
|
68 | if(_this.options.anchor !== ''){
|
69 | _this.$anchor = $('#' + _this.options.anchor);
|
70 | }else{
|
71 | _this._parsePoints();
|
72 | }
|
73 |
|
74 | _this._setSizes(function(){
|
75 | var scroll = window.pageYOffset;
|
76 | _this._calc(false, scroll);
|
77 |
|
78 | if (!_this.isStuck) {
|
79 | _this._removeSticky((scroll >= _this.topPoint) ? false : true);
|
80 | }
|
81 | });
|
82 | _this._events(id.split('-').reverse().join('-'));
|
83 | });
|
84 | }
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 | _parsePoints() {
|
92 | var top = this.options.topAnchor == "" ? 1 : this.options.topAnchor,
|
93 | btm = this.options.btmAnchor== "" ? document.documentElement.scrollHeight : this.options.btmAnchor,
|
94 | pts = [top, btm],
|
95 | breaks = {};
|
96 | for (var i = 0, len = pts.length; i < len && pts[i]; i++) {
|
97 | var pt;
|
98 | if (typeof pts[i] === 'number') {
|
99 | pt = pts[i];
|
100 | } else {
|
101 | var place = pts[i].split(':'),
|
102 | anchor = $(`#${place[0]}`);
|
103 |
|
104 | pt = anchor.offset().top;
|
105 | if (place[1] && place[1].toLowerCase() === 'bottom') {
|
106 | pt += anchor[0].getBoundingClientRect().height;
|
107 | }
|
108 | }
|
109 | breaks[i] = pt;
|
110 | }
|
111 |
|
112 |
|
113 | this.points = breaks;
|
114 | return;
|
115 | }
|
116 |
|
117 | |
118 |
|
119 |
|
120 |
|
121 |
|
122 | _events(id) {
|
123 | var _this = this,
|
124 | scrollListener = this.scrollListener = `scroll.zf.${id}`;
|
125 | if (this.isOn) { return; }
|
126 | if (this.canStick) {
|
127 | this.isOn = true;
|
128 | $(window).off(scrollListener)
|
129 | .on(scrollListener, function(e) {
|
130 | if (_this.scrollCount === 0) {
|
131 | _this.scrollCount = _this.options.checkEvery;
|
132 | _this._setSizes(function() {
|
133 | _this._calc(false, window.pageYOffset);
|
134 | });
|
135 | } else {
|
136 | _this.scrollCount--;
|
137 | _this._calc(false, window.pageYOffset);
|
138 | }
|
139 | });
|
140 | }
|
141 |
|
142 | this.$element.off('resizeme.zf.trigger')
|
143 | .on('resizeme.zf.trigger', function(e, el) {
|
144 | _this._eventsHandler(id);
|
145 | });
|
146 |
|
147 | this.$element.on('mutateme.zf.trigger', function (e, el) {
|
148 | _this._eventsHandler(id);
|
149 | });
|
150 |
|
151 | if(this.$anchor) {
|
152 | this.$anchor.on('mutateme.zf.trigger', function (e, el) {
|
153 | _this._eventsHandler(id);
|
154 | });
|
155 | }
|
156 | }
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 | _eventsHandler(id) {
|
164 | var _this = this,
|
165 | scrollListener = this.scrollListener = `scroll.zf.${id}`;
|
166 |
|
167 | _this._setSizes(function() {
|
168 | _this._calc(false);
|
169 | if (_this.canStick) {
|
170 | if (!_this.isOn) {
|
171 | _this._events(id);
|
172 | }
|
173 | } else if (_this.isOn) {
|
174 | _this._pauseListeners(scrollListener);
|
175 | }
|
176 | });
|
177 | }
|
178 |
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 | _pauseListeners(scrollListener) {
|
185 | this.isOn = false;
|
186 | $(window).off(scrollListener);
|
187 |
|
188 | |
189 |
|
190 |
|
191 |
|
192 |
|
193 | this.$element.trigger('pause.zf.sticky');
|
194 | }
|
195 |
|
196 | |
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | _calc(checkSizes, scroll) {
|
203 | if (checkSizes) { this._setSizes(); }
|
204 |
|
205 | if (!this.canStick) {
|
206 | if (this.isStuck) {
|
207 | this._removeSticky(true);
|
208 | }
|
209 | return false;
|
210 | }
|
211 |
|
212 | if (!scroll) { scroll = window.pageYOffset; }
|
213 |
|
214 | if (scroll >= this.topPoint) {
|
215 | if (scroll <= this.bottomPoint) {
|
216 | if (!this.isStuck) {
|
217 | this._setSticky();
|
218 | }
|
219 | } else {
|
220 | if (this.isStuck) {
|
221 | this._removeSticky(false);
|
222 | }
|
223 | }
|
224 | } else {
|
225 | if (this.isStuck) {
|
226 | this._removeSticky(true);
|
227 | }
|
228 | }
|
229 | }
|
230 |
|
231 | |
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | _setSticky() {
|
239 | var _this = this,
|
240 | stickTo = this.options.stickTo,
|
241 | mrgn = stickTo === 'top' ? 'marginTop' : 'marginBottom',
|
242 | notStuckTo = stickTo === 'top' ? 'bottom' : 'top',
|
243 | css = {};
|
244 |
|
245 | css[mrgn] = `${this.options[mrgn]}em`;
|
246 | css[stickTo] = 0;
|
247 | css[notStuckTo] = 'auto';
|
248 | this.isStuck = true;
|
249 | this.$element.removeClass(`is-anchored is-at-${notStuckTo}`)
|
250 | .addClass(`is-stuck is-at-${stickTo}`)
|
251 | .css(css)
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 | .trigger(`sticky.zf.stuckto:${stickTo}`);
|
258 | this.$element.on("transitionend webkitTransitionEnd oTransitionEnd otransitionend MSTransitionEnd", function() {
|
259 | _this._setSizes();
|
260 | });
|
261 | }
|
262 |
|
263 | |
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | _removeSticky(isTop) {
|
272 | var stickTo = this.options.stickTo,
|
273 | stickToTop = stickTo === 'top',
|
274 | css = {},
|
275 | anchorPt = (this.points ? this.points[1] - this.points[0] : this.anchorHeight) - this.elemHeight,
|
276 | mrgn = stickToTop ? 'marginTop' : 'marginBottom',
|
277 | notStuckTo = stickToTop ? 'bottom' : 'top',
|
278 | topOrBottom = isTop ? 'top' : 'bottom';
|
279 |
|
280 | css[mrgn] = 0;
|
281 |
|
282 | css['bottom'] = 'auto';
|
283 | if(isTop) {
|
284 | css['top'] = 0;
|
285 | } else {
|
286 | css['top'] = anchorPt;
|
287 | }
|
288 |
|
289 | this.isStuck = false;
|
290 | this.$element.removeClass(`is-stuck is-at-${stickTo}`)
|
291 | .addClass(`is-anchored is-at-${topOrBottom}`)
|
292 | .css(css)
|
293 | |
294 |
|
295 |
|
296 |
|
297 |
|
298 | .trigger(`sticky.zf.unstuckfrom:${topOrBottom}`);
|
299 | }
|
300 |
|
301 | |
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | _setSizes(cb) {
|
308 | this.canStick = MediaQuery.is(this.options.stickyOn);
|
309 | if (!this.canStick) {
|
310 | if (cb && typeof cb === 'function') { cb(); }
|
311 | }
|
312 | var _this = this,
|
313 | newElemWidth = this.$container[0].getBoundingClientRect().width,
|
314 | comp = window.getComputedStyle(this.$container[0]),
|
315 | pdngl = parseInt(comp['padding-left'], 10),
|
316 | pdngr = parseInt(comp['padding-right'], 10);
|
317 |
|
318 | if (this.$anchor && this.$anchor.length) {
|
319 | this.anchorHeight = this.$anchor[0].getBoundingClientRect().height;
|
320 | } else {
|
321 | this._parsePoints();
|
322 | }
|
323 |
|
324 | this.$element.css({
|
325 | 'max-width': `${newElemWidth - pdngl - pdngr}px`
|
326 | });
|
327 |
|
328 | var newContainerHeight = this.$element[0].getBoundingClientRect().height || this.containerHeight;
|
329 | if (this.$element.css("display") == "none") {
|
330 | newContainerHeight = 0;
|
331 | }
|
332 | this.containerHeight = newContainerHeight;
|
333 | this.$container.css({
|
334 | height: newContainerHeight
|
335 | });
|
336 | this.elemHeight = newContainerHeight;
|
337 |
|
338 | if (!this.isStuck) {
|
339 | if (this.$element.hasClass('is-at-bottom')) {
|
340 | var anchorPt = (this.points ? this.points[1] - this.$container.offset().top : this.anchorHeight) - this.elemHeight;
|
341 | this.$element.css('top', anchorPt);
|
342 | }
|
343 | }
|
344 |
|
345 | this._setBreakPoints(newContainerHeight, function() {
|
346 | if (cb && typeof cb === 'function') { cb(); }
|
347 | });
|
348 | }
|
349 |
|
350 | |
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 | _setBreakPoints(elemHeight, cb) {
|
357 | if (!this.canStick) {
|
358 | if (cb && typeof cb === 'function') { cb(); }
|
359 | else { return false; }
|
360 | }
|
361 | var mTop = emCalc(this.options.marginTop),
|
362 | mBtm = emCalc(this.options.marginBottom),
|
363 | topPoint = this.points ? this.points[0] : this.$anchor.offset().top,
|
364 | bottomPoint = this.points ? this.points[1] : topPoint + this.anchorHeight,
|
365 |
|
366 |
|
367 | winHeight = window.innerHeight;
|
368 |
|
369 | if (this.options.stickTo === 'top') {
|
370 | topPoint -= mTop;
|
371 | bottomPoint -= (elemHeight + mTop);
|
372 | } else if (this.options.stickTo === 'bottom') {
|
373 | topPoint -= (winHeight - (elemHeight + mBtm));
|
374 | bottomPoint -= (winHeight - mBtm);
|
375 | } else {
|
376 |
|
377 | }
|
378 |
|
379 | this.topPoint = topPoint;
|
380 | this.bottomPoint = bottomPoint;
|
381 |
|
382 | if (cb && typeof cb === 'function') { cb(); }
|
383 | }
|
384 |
|
385 | |
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 | _destroy() {
|
392 | this._removeSticky(true);
|
393 |
|
394 | this.$element.removeClass(`${this.options.stickyClass} is-anchored is-at-top`)
|
395 | .css({
|
396 | height: '',
|
397 | top: '',
|
398 | bottom: '',
|
399 | 'max-width': ''
|
400 | })
|
401 | .off('resizeme.zf.trigger')
|
402 | .off('mutateme.zf.trigger');
|
403 | if (this.$anchor && this.$anchor.length) {
|
404 | this.$anchor.off('change.zf.sticky');
|
405 | }
|
406 | $(window).off(this.scrollListener);
|
407 |
|
408 | if (this.wasWrapped) {
|
409 | this.$element.unwrap();
|
410 | } else {
|
411 | this.$container.removeClass(this.options.containerClass)
|
412 | .css({
|
413 | height: ''
|
414 | });
|
415 | }
|
416 | }
|
417 | }
|
418 |
|
419 | Sticky.defaults = {
|
420 | |
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 | container: '<div data-sticky-container></div>',
|
427 | |
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 | stickTo: 'top',
|
434 | |
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 | anchor: '',
|
441 | |
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 | topAnchor: '',
|
448 | |
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 | btmAnchor: '',
|
455 | |
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 | marginTop: 1,
|
462 | |
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 | marginBottom: 1,
|
469 | |
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 | stickyOn: 'medium',
|
476 | |
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 | stickyClass: 'sticky',
|
483 | |
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 | containerClass: 'sticky-container',
|
490 | |
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 | checkEvery: -1
|
497 | };
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 | function emCalc(em) {
|
504 | return parseInt(window.getComputedStyle(document.body, null).fontSize, 10) * em;
|
505 | }
|
506 |
|
507 | export {Sticky};
|