Skip to content

Commit 522f1ab

Browse files
authored
Update script.js
1 parent 816042c commit 522f1ab

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

js/script.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@ document.addEventListener("DOMContentLoaded", function () {
33
const contributorsList = document.getElementById("contributorsList");
44
const maxVisibleContributors = 5;
55

6-
if (toggleContributorsLink) {
6+
if (toggleContributorsLink && contributorsList) {
77
const contributors = Array.from(contributorsList.getElementsByClassName("contributor-item"));
88
const totalContributors = contributors.length;
99

10+
// Initialize: Set the correct state for the button and list
11+
const initState = toggleContributorsLink.textContent.includes("more");
12+
if (initState) {
13+
contributors.forEach((contributor, index) => {
14+
if (index >= maxVisibleContributors) {
15+
contributor.classList.add("d-none");
16+
}
17+
});
18+
}
19+
1020
// Function to toggle contributors visibility
1121
function toggleContributors() {
1222
const isShowingAll = toggleContributorsLink.textContent === "View Less";
13-
14-
// Show or hide contributors based on the current state
23+
24+
// Toggle visibility
1525
contributors.forEach((contributor, index) => {
1626
if (isShowingAll || index < maxVisibleContributors) {
1727
contributor.classList.remove("d-none");
@@ -21,27 +31,15 @@ document.addEventListener("DOMContentLoaded", function () {
2131
});
2232

2333
// Update the button text
24-
if (isShowingAll) {
25-
toggleContributorsLink.textContent = `+${totalContributors - maxVisibleContributors} more Contributors`;
26-
} else {
27-
toggleContributorsLink.textContent = "View Less";
28-
}
34+
toggleContributorsLink.textContent = isShowingAll
35+
? `+${totalContributors - maxVisibleContributors} more Contributors`
36+
: "View Less";
2937
}
3038

31-
// Initialize the state (start with showing only the first 5)
32-
contributors.forEach((contributor, index) => {
33-
if (index >= maxVisibleContributors) {
34-
contributor.classList.add("d-none");
35-
}
36-
});
37-
38-
// Set the initial button text
39-
toggleContributorsLink.textContent = `+${totalContributors - maxVisibleContributors} more Contributors`;
40-
4139
// Add the click event listener
4240
toggleContributorsLink.addEventListener("click", function (e) {
4341
e.preventDefault();
44-
toggleContributors(); // Toggle visibility and update text
42+
toggleContributors();
4543
});
4644
}
4745
});

0 commit comments

Comments
 (0)