-
Notifications
You must be signed in to change notification settings - Fork 0
/
flair.js
96 lines (71 loc) · 2.68 KB
/
flair.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
( function() {
var links;
document.addEventListener('DOMContentLoaded', initFlair);
function initFlair() {
/** add hover x and y coordinates to links and buttons **/
links = document.querySelectorAll('a, button, .button');
links.forEach(function(el) {
el.addEventListener( 'mousemove', buttonFlair );
});
//** add intersection data to images and major sections **/
if('IntersectionObserver' in window){
let options = {
root: null,
rootMargin: '0px',
threshold: buildThreshold( 100 )
}
let observer = new IntersectionObserver(observerCallback, options);
let els = document.querySelectorAll(".wp-block-cover, img, header, footer, nav, section, .flair-io");
els.forEach(function(el) {
observer.observe(el);
el.dataset.wasVisible = false;
el.style.setProperty( '--was-visible', 'false' );
});
}
// setInterval(changeRando, 1000);
}
function buttonFlair(e) {
const x = e.pageX - e.target.offsetX;
const y = e.pageY - e.target.offsetY;
e.target.style.setProperty( '--mouse-x', e.offsetX );
e.target.style.setProperty( '--mouse-y', e.offsetY );
e.target.style.setProperty( '--mouse-x-pct', e.offsetX / e.target.offsetWidth );
e.target.style.setProperty( '--mouse-y-pct', e.offsetY / e.target.offsetHeight );
}
/**
* Seeds random numbers to css vars
*/
function changeRando() {
document.querySelector(':root').style.setProperty('--random', Math.random());
document.querySelector(':root').style.setProperty('--random2', Math.random());
}
function observerCallback(entries, observer) {
entries.forEach(function(entry) {
var top = entry.boundingClientRect.top;
var height = entry.boundingClientRect.height;
var width = entry.boundingClientRect.width;
var pct = 0;
var vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
// set the distance from the top of the element to the top of the viewport
entry.target.style.setProperty( '--from-top', top );
entry.target.style.setProperty( '--height', height );
entry.target.style.setProperty( '--width', width );
entry.target.dataset.isIntersecting = entry.isIntersecting;
entry.target.style.setProperty( '--intersecting', entry.isIntersecting );
if( entry.target.dataset.wasVisible == true || entry.isIntersecting ) {
entry.target.dataset.wasVisible = true;
entry.target.style.setProperty( '--was-visible', true );
}
entry.target.style.setProperty( '--intersection-ratio', entry.intersectionRatio );
});
}
function buildThreshold(num) {
let thresholds = [];
for (let i=1.0; i<=num; i++) {
let ratio = i/num;
thresholds.push(ratio);
}
thresholds.push(0);
return thresholds;
}
})();