-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcta.js
57 lines (47 loc) · 1.57 KB
/
cta.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
// Hide CTA's on header when stuck
(function () {
createStuckCSSClass();
const navbar = document.getElementById('Navbar');
// Set sentinel on body after the navbar
const sentinel = (function putSentinel(debugMode) {
const sentinelElement = generateSentinelElement();
insertBefore(sentinelElement, navbar);
return sentinelElement;
function generateSentinelElement() {
const sentinelEl = document.createElement('div');
sentinelEl.style.height = '100px';
sentinelEl.style.width = '100%';
sentinelEl.style.position = 'absolute';
sentinelEl.style.visibility = 'hidden';
if (debugMode) {
sentinelEl.style.backgroundColor = 'rgba(255, 0, 0, 0.5)';
sentinelEl.style.visibility = 'unset';
sentinelEl.style.zIndex = 10000000;
}
return sentinelEl;
}
function insertBefore(el, referenceNode) {
referenceNode.parentNode.insertBefore(el, referenceNode);
}
})();
const observer = new IntersectionObserver(records => onAppears(records), {
threshold: [0, 1],
});
observer.observe(sentinel);
function onAppears(entries) {
if (entries[0].intersectionRatio === 0) {
navbar.classList.add('stuck');
} else if (entries[0].intersectionRatio === 1) {
navbar.classList.remove('stuck');
}
}
function createStuckCSSClass() {
const stuckStyle = document.createElement('style');
stuckStyle.innerHTML = `
.stuck [data-hide-when-stuck] {
display: none;
}
`;
document.getElementsByTagName('head')[0].appendChild(stuckStyle);
}
})();