-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (74 loc) · 2.75 KB
/
index.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
/* global window document */
const SOS = {
scrollTimeout: 100,
watchStickerTimeout: undefined,
assistedPositionRules: [
'display',
'width', 'height',
'margin', 'margin-top', 'margin-right', 'margin-bottom', 'margin-left',
],
genEventDetail: (sticky) => ({ detail: { sticky }, bubbles: true }),
addPlaceholder(sticker) {
const stickerStyle = window.getComputedStyle(sticker);
const placeholder = document.createElement('div');
for (let i = stickerStyle.length - 1; i >= 0; i--) {
const rule = stickerStyle[i];
if (SOS.assistedPositionRules.indexOf(rule) > -1) {
placeholder.style[rule] = stickerStyle.getPropertyValue(rule);
}
}
/* eslint-disable no-param-reassign */
sticker.parentNode.insertBefore(placeholder, sticker);
sticker.placeholder = placeholder;
sticker.className += ' sticky';
/* eslint-enable no-param-reassign */
const evt = new window.CustomEvent('stickyToggle', SOS.genEventDetail(true));
sticker.dispatchEvent(evt);
},
removePlaceholder(sticker) {
/* eslint-disable no-param-reassign */
if (sticker.placeholder) {
sticker.parentNode.removeChild(sticker.placeholder);
sticker.placeholder = undefined;
}
sticker.className = sticker.className.replace(/(\b)sticky(\b)/g, '$1$2').trim();
/* eslint-enable no-param-reassign */
const evt = new window.CustomEvent('stickyToggle', SOS.genEventDetail(false));
sticker.dispatchEvent(evt);
},
watchSticker(evt, sticker) {
this.watchStickerTimeout = undefined;
const { offsetHeight, bottomRef } = sticker;
const top = (sticker.placeholder || sticker).getBoundingClientRect().top;
const isAfterSticker = top <= 0;
const isAfterRef = bottomRef && bottomRef.getBoundingClientRect().bottom < offsetHeight;
const isInsideArea = isAfterSticker && !isAfterRef;
if (!sticker.placeholder && isInsideArea) {
this.addPlaceholder(sticker);
} else if (sticker.placeholder && !isInsideArea) {
this.removePlaceholder(sticker);
}
},
stickOnScroll(sticker) {
if (sticker.stick) {
return;
}
/* eslint-disable no-param-reassign */
sticker.stick = (e) => {
if (!this.watchStickerTimeout) {
this.watchStickerTimeout = window.setTimeout(() => {
this.watchSticker(e, sticker);
}, this.scrollTimeout);
}
};
sticker.bottomRef = document.querySelector(sticker.getAttribute('data-fos-bottomref'));
/* eslint-enable no-param-reassign */
window.addEventListener('scroll', sticker.stick);
},
discoverAll() {
const stickOnScroll = ::this.stickOnScroll;
const elements = [].slice.call(document.querySelectorAll('[data-fos]'), 0);
elements.forEach(stickOnScroll);
},
};
export default SOS;