-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrain.js
64 lines (52 loc) · 1.95 KB
/
grain.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
let activateGrainOnLoad = false; // Set to true or false based on your preference
// Get the icon element
const toggleIcon = document.querySelector('.sidebar_menu-grain-toggle');
// Initialize a boolean variable to keep track of the state
let isGrainedVisible = false;
// Define the options for the grained function
const options = {
animate: true,
patternWidth: 485,
patternHeight: 485,
grainOpacity: 0.05,
grainDensity: 1,
grainWidth: 1.1,
grainHeight: 1.1
};
// Function to apply the grained effect to elements
const applyGrainedEffect = () => {
const grainBody = document.querySelector('#grain-body');
const grainSidebar = document.querySelector('#grain-sidebar');
const grainProfile = document.querySelector('#grain-profile');
if (grainBody) {
grained('#grain-body', options);
grainBody.style.position = 'fixed';
grainBody.style.display = 'none'; // Hide the grained body initially
}
if (grainSidebar) {
grained('#grain-sidebar', options);
}
if (grainProfile) {
grained('#grain-profile', options);
}
if (activateGrainOnLoad) {
// Show the grained body element if activateGrainOnLoad is true
grainBody.style.display = 'block';
}
};
// Function to show/hide the grained body element
const toggleGrainedVisibility = () => {
const grainBody = document.querySelector('#grain-body');
isGrainedVisible = !isGrainedVisible; // Toggle the state
// Show/hide the grained body element based on the state
grainBody.style.display = isGrainedVisible ? 'block' : 'none';
// Update the toggle button opacity
toggleIcon.style.opacity = isGrainedVisible ? 1 : 0.5;
// Toggle the active/inactive class on the button
toggleIcon.classList.toggle('active', isGrainedVisible);
toggleIcon.classList.toggle('inactive', !isGrainedVisible);
};
// Apply the grained effect to elements on page load
applyGrainedEffect();
// Add an event listener to the icon element
toggleIcon.addEventListener('click', toggleGrainedVisibility);