-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
123 lines (92 loc) · 3.4 KB
/
script.js
File metadata and controls
123 lines (92 loc) · 3.4 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const contextMenu = document.getElementById("context-menu");
const scope = document.querySelector("body");
const reload = document.getElementById("reloadPage");
const printElement = document.getElementById("print");
const savePage = document.getElementById("savePage");
const shareContextMenuButton = document.getElementById("shareContextMenuButton");
const shareButton = document.getElementById("shareButton");
// The Ripple Animation for the Button
const buttons = document.getElementsByTagName("button");
function createRipple(event) {
const button = event.currentTarget;
const circle = document.createElement("span");
const diameter = Math.max(button.clientWidth, button.clientHeight);
const radius = diameter / 2;
circle.style.width = circle.style.height = `${diameter}px`;
circle.style.left = `${event.clientX - button.offsetLeft - radius}px`;
circle.style.top = `${event.clientY - button.offsetTop - radius}px`;
circle.classList.add("ripple");
const ripple = button.getElementsByClassName("ripple")[0];
if (ripple) {
ripple.remove();
}
button.appendChild(circle);
}
// Loop through all the buttons on the website and apply the ripple effect to each one
for (const button of buttons) {
button.addEventListener("click", createRipple);
}
const normalizePozition = (mouseX, mouseY) => {
// ? compute what is the mouse position relative to the container element (scope)
let {
left: scopeOffsetX,
top: scopeOffsetY,
} = scope.getBoundingClientRect();
scopeOffsetX = scopeOffsetX < 0 ? 0 : scopeOffsetX;
scopeOffsetY = scopeOffsetY < 0 ? 0 : scopeOffsetY;
const scopeX = mouseX - scopeOffsetX;
const scopeY = mouseY - scopeOffsetY;
// ? check if the element will go out of bounds
const outOfBoundsOnX = scopeX + contextMenu.clientWidth > scope.clientWidth;
const outOfBoundsOnY =
scopeY + contextMenu.clientHeight > scope.clientHeight;
let normalizedX = mouseX;
let normalizedY = mouseY;
// ? normalize on X
if (outOfBoundsOnX) {
normalizedX =
scopeOffsetX + scope.clientWidth - contextMenu.clientWidth;
}
// ? normalize on Y
if (outOfBoundsOnY) {
normalizedY =
scopeOffsetY + scope.clientHeight - contextMenu.clientHeight;
}
return { normalizedX, normalizedY };
};
scope.addEventListener("contextmenu", (event) => {
event.preventDefault();
const { clientX: mouseX, clientY: mouseY } = event;
const { normalizedX, normalizedY } = normalizePozition(mouseX, mouseY);
contextMenu.classList.add("visible");
contextMenu.style.top = `${normalizedY}px`;
contextMenu.style.left = `${normalizedX}px`;
// setTimeout(() => {
// contextMenu.classList.add("visible");
// });
});
scope.addEventListener("click", (e) => {
if (e.target.offsetParent != contextMenu) {
contextMenu.classList.remove("visible");
}
});
reload.addEventListener("click", (event) => {
location.reload();
})
printElement.addEventListener("click", (event) => {
window.print();
})
shareContextMenuButton.addEventListener("click", (event) => {
navigator.share({
title: document.title,
text: "Check out this really cool project that @ValiantWind made!!1!111!!",
url: window.location.href
})
});
shareButton.addEventListener("click", (event) => {
navigator.share({
title: document.title,
text: "Check out this really cool project that @ValiantWind made!!1!111!!",
url: window.location.href
})
});