Skip to content

Commit 51ce68e

Browse files
authored
Update script.js
1 parent 02e0d70 commit 51ce68e

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

js/script.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
1-
document.addEventListener("DOMContentLoaded", function () {
2-
const contributorsList = document.querySelector("#contributorsList");
1+
document.addEventListener("DOMContentLoaded", function () {
32
const toggleContributorsLink = document.getElementById("toggleContributors");
3+
const contributorsList = document.getElementById("contributorsList");
44
const maxVisibleContributors = 5;
5+
6+
// Only proceed if there are more than maxVisibleContributors
7+
if (toggleContributorsLink) {
8+
const contributors = Array.from(contributorsList.getElementsByClassName("contributor-item"));
9+
const totalContributors = contributors.length;
510

6-
const contributors = Array.from(contributorsList.getElementsByClassName("contributor-item"));
7-
const totalContributors = contributors.length;
11+
// Set the initial state to show only the first 5 contributors
12+
function toggleContributors() {
13+
const isShowingAll = toggleContributorsLink.textContent === "View Less";
14+
15+
// Show or hide contributors beyond the first 5
16+
contributors.forEach((contributor, index) => {
17+
if (isShowingAll || index < maxVisibleContributors) {
18+
contributor.classList.remove("d-none");
19+
} else {
20+
contributor.classList.add("d-none");
21+
}
22+
});
823

9-
// Function to toggle between showing more or fewer contributors
10-
function toggleContributors() {
11-
const isShowingAll = toggleContributorsLink.textContent === "View Less";
12-
13-
// Show or hide contributors based on the toggle state
14-
contributors.forEach((contributor, index) => {
15-
if (isShowingAll || index < maxVisibleContributors) {
16-
contributor.classList.remove("d-none");
24+
// Toggle the link text based on the current state
25+
if (isShowingAll) {
26+
toggleContributorsLink.textContent = `+${totalContributors - maxVisibleContributors} more Contributors`;
1727
} else {
18-
contributor.classList.add("d-none");
28+
toggleContributorsLink.textContent = "View Less";
1929
}
20-
});
21-
22-
// Toggle the link text based on the current state
23-
if (isShowingAll) {
24-
toggleContributorsLink.textContent = `+${totalContributors - maxVisibleContributors} more Contributors`;
25-
} else {
26-
toggleContributorsLink.textContent = "View Less";
2730
}
28-
}
2931

30-
// Click event for the "View More / View Less" link
31-
if (toggleContributorsLink) {
32+
// Initial call to ensure the first 5 contributors are shown
33+
toggleContributors();
34+
35+
// Event listener for clicking the toggle link
3236
toggleContributorsLink.addEventListener("click", function (e) {
3337
e.preventDefault();
34-
toggleContributors(); // Toggle the contributors and text
38+
toggleContributors(); // Toggle contributors and update link text
3539
});
3640
}
3741
});

0 commit comments

Comments
 (0)