Skip to content

Commit

Permalink
Merge pull request #1550 from Raj-Aditya-27/newBranch
Browse files Browse the repository at this point in the history
fix: Scroll back button smoothly scrolling issue
  • Loading branch information
apu52 authored Oct 26, 2024
2 parents 849b8f5 + 63dabc0 commit 2439180
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2077,24 +2077,41 @@ <h4>Contact Us</h4>
<a class="goupbtn" href="#" onclick="goToTop()"><i class="fa-solid fa-arrow-up"></i></a>

<script>
// Get the button
var mybutton = document.querySelector(".goupbtn");

// When the user scrolls, check if the button should be displayed

window.addEventListener("scroll", function () {
if (window.scrollY > 20) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
});

// When the user clicks on the button, scroll to the top of the document

function goToTop() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
const currentScroll = window.scrollY;
const startTime = performance.now();
const duration = 500;

function scrollAnimation(currentTime) {
const elapsedTime = currentTime - startTime;
const progress = Math.min(elapsedTime / duration, 1);
const easeOut = progress * (2 - progress);
const scrollPosition = currentScroll * (1 - easeOut);

window.scrollTo(0, scrollPosition);

if (progress < 1) {
requestAnimationFrame(scrollAnimation);
}
}

requestAnimationFrame(scrollAnimation);
}

mybutton.addEventListener('click', goToTop);
</script>





Expand Down

0 comments on commit 2439180

Please sign in to comment.