Skip to content

Commit 70d5aa6

Browse files
authored
Merge pull request #10 from Krishna-Sri-Charan/charan-scroll-button
Add Scroll To Top and Bottom button
2 parents b7a34e8 + 95a2956 commit 70d5aa6

3 files changed

Lines changed: 110 additions & 0 deletions

File tree

docs/css/style.css

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,4 +1205,51 @@ html {
12051205

12061206
section {
12071207
scroll-margin-top: 80px;
1208+
}
1209+
1210+
/* Scroll to Top/Bottom Buttons */
1211+
.scroll-btn {
1212+
position: fixed;
1213+
right: var(--space-5); /* 1.25rem */
1214+
width: 50px;
1215+
height: 50px;
1216+
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
1217+
color: var(--white);
1218+
border: none;
1219+
border-radius: 50%;
1220+
display: flex;
1221+
align-items: center;
1222+
justify-content: center;
1223+
cursor: pointer;
1224+
box-shadow: var(--shadow-lg);
1225+
z-index: 100;
1226+
opacity: 0;
1227+
visibility: hidden;
1228+
transform: translateY(20px);
1229+
transition: all var(--transition-normal);
1230+
}
1231+
1232+
.scroll-btn:hover {
1233+
transform: scale(1.1);
1234+
box-shadow: var(--shadow-xl);
1235+
}
1236+
1237+
.scroll-btn.visible {
1238+
opacity: 1;
1239+
visibility: visible;
1240+
transform: translateY(0);
1241+
}
1242+
1243+
#scrollTopBtn {
1244+
bottom: var(--space-5);
1245+
}
1246+
1247+
#scrollBottomBtn {
1248+
/* Position 60px above the top button (50px height + 10px gap) */
1249+
bottom: calc(var(--space-5) + 60px);
1250+
}
1251+
1252+
.scroll-btn svg {
1253+
width: 28px;
1254+
height: 28px;
12081255
}

docs/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,17 @@ <h2 class="download-title">Ready to Boost Your Productivity?</h2>
377377
</div>
378378
</section>
379379

380+
<button id="scrollTopBtn" class="scroll-btn" aria-label="Scroll to top" title="Scroll to top">
381+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
382+
<path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6 1.41 1.41z"/>
383+
</svg>
384+
</button>
385+
<button id="scrollBottomBtn" class="scroll-btn" aria-label="Scroll to bottom" title="Scroll to bottom">
386+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
387+
<path d="M7.41 8.59L12 13.17l4.59-4.58L18 10l-6 6-6-6 1.41-1.41z"/>
388+
</svg>
389+
</button>
390+
380391
<!-- Footer -->
381392
<footer class="footer">
382393
<div class="container">

docs/js/main.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,58 @@ document.addEventListener('DOMContentLoaded', function() {
338338

339339
// Initialize carousel
340340
carousel.init();
341+
342+
// Get the button elements from the HTML
343+
const scrollTopBtn = document.getElementById('scrollTopBtn');
344+
const scrollBottomBtn = document.getElementById('scrollBottomBtn');
345+
346+
// Function to handle the visibility of both scroll buttons
347+
const handleScrollButtons = () => {
348+
const scrollHeight = document.documentElement.scrollHeight;
349+
const clientHeight = document.documentElement.clientHeight;
350+
const scrollPosition = window.scrollY;
351+
352+
// --- Scroll to Top Button Logic ---
353+
// Show the button if user has scrolled down more than 300px
354+
if (scrollPosition > 300) {
355+
scrollTopBtn.classList.add('visible');
356+
} else {
357+
scrollTopBtn.classList.remove('visible');
358+
}
359+
360+
// --- Scroll to Bottom Button Logic ---
361+
// Show the button if user is near the top, but hide it when they get close to the bottom
362+
const isNearBottom = scrollPosition + clientHeight >= scrollHeight - 300;
363+
if (scrollPosition > 100 && !isNearBottom) {
364+
scrollBottomBtn.classList.add('visible');
365+
} else {
366+
scrollBottomBtn.classList.remove('visible');
367+
}
368+
};
369+
370+
// --- Click Event Listeners ---
371+
372+
// Smoothly scroll to the top of the page
373+
scrollTopBtn.addEventListener('click', () => {
374+
window.scrollTo({
375+
top: 0,
376+
behavior: 'smooth'
377+
});
378+
});
379+
380+
// Smoothly scroll to the bottom of the page
381+
scrollBottomBtn.addEventListener('click', () => {
382+
window.scrollTo({
383+
top: document.body.scrollHeight,
384+
behavior: 'smooth'
385+
});
386+
});
387+
388+
// Add a scroll event listener to the window to check button visibility
389+
window.addEventListener('scroll', handleScrollButtons);
390+
391+
// Initial check when the page loads
392+
handleScrollButtons();
341393
});
342394

343395
// Add some CSS classes for enhanced animations

0 commit comments

Comments
 (0)