Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
QuiteAFancyEmerald
GitHub Repository: QuiteAFancyEmerald/Holy-Unblocker
Path: blob/master/views/assets/js/card.js
5247 views
1
/* -----------------------------------------------
2
/* Authors: QuiteAFancyEmerald
3
/* GNU Affero General Public License v3.0: https://www.gnu.org/licenses/agpl-3.0.en.html
4
/* Card Shimmer Mouse Follow Script
5
/* ----------------------------------------------- */
6
7
// Encase everything in a new scope so that variables are not accidentally
8
// attached to the global scope.
9
(() => {
10
// Track the cursor position with respect to the top left of the card.
11
// The "this" keyword gets the element that invoked the event listener.
12
const handleMouseMove = (element) => {
13
element.addEventListener('mousemove', (e) => {
14
const rect = element.getBoundingClientRect();
15
const x = e.clientX - rect.left;
16
const y = e.clientY - rect.top;
17
18
element.style.setProperty('--mouse-x', `${x}px`);
19
element.style.setProperty('--mouse-y', `${y}px`);
20
});
21
},
22
// Reset the cursor tracking variables when the cursor leaves the card.
23
handleMouseLeave = (element) => {
24
element.addEventListener('mouseleave', () => {
25
element.style.setProperty('--mouse-x', `50%`);
26
element.style.setProperty('--mouse-y', `50%`);
27
});
28
},
29
// Get the box card elements and add the event listeners to them.
30
shimmerEffects = document.querySelectorAll('.box-card, .box-hero');
31
32
/* Attach CSS variables, mouse-x and mouse-y, to elements that will be
33
* given shimmer effects, by adding or modifying the style attribute.
34
* CSS calculates and renders the actual shimmer effect from there.
35
*/
36
shimmerEffects.forEach(handleMouseMove);
37
shimmerEffects.forEach(handleMouseLeave);
38
})();
39
40