Skip to content

Commit

Permalink
Merge pull request #38 from Sanketpatil27/add-back-to-top-button
Browse files Browse the repository at this point in the history
Added 'Back to Top' button for smooth page navigation, Solved issue: #22
  • Loading branch information
JollyJolli authored Oct 2, 2024
2 parents d15b620 + 2929393 commit 4f72b0c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,24 @@ function initializeSearch() {
searchButton.addEventListener('click', () => {
displayContributors(searchInput.value);
});
}
}

// Get the button element
const backToTopBtn = document.getElementById('backToTopBtn');

// Show the button when the user scrolls down 100px from the top
window.onscroll = function () {
if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
backToTopBtn.style.display = "block";
} else {
backToTopBtn.style.display = "none";
}
};

// Smooth scroll to the top when the button is clicked
backToTopBtn.addEventListener('click', function () {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
3 changes: 3 additions & 0 deletions src/contributors.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@
},
{
"name": "Bupendra"
},
{
"name": "Sanket"
}
]
3 changes: 3 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ <h2>Participants</h2>
</p>
</footer>

<!-- Back to Top Button -->
<button title="back to top" id="backToTopBtn" aria-label="Back to Top"></button>

<script src="app.js"></script>
<script src="https://rawgit.com/WeiChiaChang/Easter-egg/master/easter-eggs-collection.js"></script>
</body>
Expand Down
36 changes: 36 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,42 @@ footer a:hover {
margin-top: 1rem;
}

/* Back to Top Button Styles */
#backToTopBtn {
position: fixed;
bottom: 40px;
right: 40px;
background-color: var(--accent-light); /* Green accent in light mode */
color: var(--text-light);
border: none;
border-radius: 50px;
padding: 15px 20px;
font-size: 1.5rem;
cursor: pointer;
display: none; /* Hidden by default */
z-index: 1000;
transition: background-color 0.3s ease, color 0.3s ease, transform 0.3s;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

#backToTopBtn:hover {
background-color: var(--bg-dark); /* Dark green on hover */
color: var(--text-dark); /* Light text on hover */
transform: scale(1.1); /* Button enlarges slightly */
}

/* Dark mode styles */
body.dark-mode #backToTopBtn {
background-color: var(--accent-dark); /* Pink accent in dark mode */
color: var(--text-dark);
}

body.dark-mode #backToTopBtn:hover {
background-color: var(--bg-light); /* Light background on hover */
color: var(--text-light); /* Dark text on hover */
}


@keyframes spin {
0% {
transform: rotate(0deg);
Expand Down

0 comments on commit 4f72b0c

Please sign in to comment.