Skip to content

Commit 324789b

Browse files
authored
Merge pull request #31 from rajasmitaa/scroll-to-top
Scroll-to-top button feature
2 parents 16ef48f + 6cd8581 commit 324789b

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

index.html

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,47 @@ <h3>Sara Ahmed</h3>
303303
<p>Built with ❤️ for showcasing amazing front-end projects</p>
304304
</div>
305305
</footer>
306+
<button id="scrollToTopBtn" title="Go to top"></button>
307+
<script src="script.js"></script>
308+
<script>
309+
const toggle = document.getElementById('darkModeToggle');
310+
const body = document.body;
311+
const icon = document.getElementById('themeIcon');
306312

313+
// Load preference
314+
const savedTheme = localStorage.getItem('theme');
315+
if (savedTheme === 'dark') {
316+
body.classList.add('dark-theme');
317+
icon.textContent = '☀️'; // Sun in dark mode
318+
} else {
319+
icon.textContent = '🌙'; // Moon in light mode
320+
}
307321

308-
<script src="script.js"></script>
309-
</body>
310-
</html>
322+
toggle.addEventListener('click', () => {
323+
body.classList.toggle('dark-theme');
324+
const theme = body.classList.contains('dark-theme') ? 'dark' : 'light';
325+
localStorage.setItem('theme', theme);
326+
327+
// Update icon
328+
icon.textContent = theme === 'dark' ? '☀️' : '🌙';
329+
</script>
330+
331+
332+
<script>
333+
const scrollToTopBtn = document.getElementById("scrollToTopBtn");
334+
335+
// Show button when user scrolls down
336+
window.addEventListener("scroll", () => {
337+
scrollToTopBtn.style.display = window.scrollY > 300 ? "block" : "none";
338+
});
339+
340+
// Scroll to top smoothly
341+
scrollToTopBtn.addEventListener("click", () => {
342+
window.scrollTo({
343+
top: 0,
344+
behavior: "smooth"
345+
});
346+
});
347+
348+
</script>
349+
</body>

style.css

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,4 +794,26 @@ body.dark-theme .fill-btn:hover {
794794
}
795795
}
796796

797-
/* ------------------------------------------- */
797+
/* ------------------------------------------- */
798+
#scrollToTopBtn {
799+
position: fixed;
800+
bottom: 2rem;
801+
right: 2rem;
802+
z-index: 999;
803+
background-color: #667eea;
804+
color: white;
805+
border: none;
806+
outline: none;
807+
padding: 0.75rem 1rem;
808+
border-radius: 50%;
809+
font-size: 1.25rem;
810+
cursor: pointer;
811+
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
812+
display: none;
813+
transition: background 0.3s ease, transform 0.3s ease;
814+
}
815+
816+
#scrollToTopBtn:hover {
817+
background-color: #5a67d8;
818+
transform: scale(1.1);
819+
}

0 commit comments

Comments
 (0)