-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (54 loc) · 2.18 KB
/
script.js
File metadata and controls
63 lines (54 loc) · 2.18 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
// Debounce function to control rapid hover events
function debounce(func, delay) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), delay);
};
}
// Function to populate the lists
function populateList(listClass, dataArray) {
const list = document.querySelector(listClass);
const firstParagraph = document.querySelector('.description p'); // Target the first <p> element
// Extract the default description from the first <p> tag in the .description element
const defaultDescription = firstParagraph.innerHTML.trim(); // Get the innerHTML of the first <p>
// Debounced function to update the description
const updateDescription = debounce((html) => {
if (!html) {
firstParagraph.innerHTML = defaultDescription; // Reset to default if no content
return;
}
// Use DOM parsing for safer insertion of links and styles
const tempContainer = document.createElement('div');
tempContainer.innerHTML = html;
// Replace the content of the first <p> tag with the new HTML
firstParagraph.innerHTML = tempContainer.innerHTML;
}, 123); // Adjust the delay as needed
// Create list items and add event listeners
dataArray.forEach(item => {
const listItem = document.createElement('li');
const link = document.createElement('a');
link.href = item.link;
link.textContent = item.name;
// Add event listeners for hover
link.addEventListener('mouseenter', () => {
updateDescription(item.description);
});
link.addEventListener('mouseleave', () => {
updateDescription(defaultDescription);
});
listItem.appendChild(link);
list.appendChild(listItem);
});
}
// Fetch the data from the JSON file
fetch('data.json')
.then(response => response.json())
.then(data => {
// Populate the lists with the data from the JSON
populateList('.me-list', data.me);
populateList('.friend-list', data.friends);
})
.catch(error => {
console.error('Error loading the data:', error);
});