Bootstrap Scroll to Top Button
A Bootstrap Scroll To Top button, created using jQuery or JavaScript, allows users to smoothly navigate to the page's top. With jQuery, we bind a click event to a button, animating the scroll. JavaScript achieves the same by detecting the scroll position, showing the button when scrolled down, and smoothly scrolling up on click. Both methods enhance user experience and are vital for long web pages.




Thanks for your feedback!
Your contributions will help us to improve service.
How can I implement a Bootstrap Scroll To Top button using jQuery in a web page?
This code snippet creates a Bootstrap "Scroll to Top" button using jQuery. Initially hidden, the button appears as you scroll down the page. When clicked, it smoothly scrolls the page to the top. A CSS style defines the button's fixed position at the bottom-right of the screen. In the JavaScript section, an event listener monitors the scroll position. When the user scrolls past 100 pixels, the button fades in; otherwise, it fades out. The click event triggers an animation that smoothly scrolls the page's HTML and body elements to the top, creating a user-friendly "back to top" functionality for long pages.
Bootstrap Scroll To Top Button using Jquery
<style>
/* Custom CSS for the button */
#scroll-to-top {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
}
</style>
<div class="container p-3 shadow-sm">
<div style="height: 2000px;">
<h1>Bootstrap Scroll to Top of Page</h1>
<p>Scroll down to see the Scroll to Top button.</p>
</div>
<!-- Scroll to Top Button -->
<button id="scroll-to-top" class="btn btn-dark">
<i class="fa-solid fa-arrow-up"></i> <!-- Font Awesome icon for "up" -->
</button>
</div>
<script>
$(document).ready(function () {
// Show or hide the button based on the scroll position
$(window).scroll(function () {
if ($(this).scrollTop() > 100) {
$('#scroll-to-top').fadeIn();
} else {
$('#scroll-to-top').fadeOut();
}
});
// Scroll to top when the button is clicked
$('#scroll-to-top').click(function () {
$('html, body').animate({ scrollTop: 0 }, 'slow');
});
});
</script>
How can I create a Bootstrap Scroll To Top Button using JavaScript?
This code snippet creates a Bootstrap-style "Scroll to Top" button using JavaScript. Initially hidden, the button becomes visible when the user scrolls 20 pixels down the page. The button is placed at the bottom right corner of the page. When clicked, it smoothly scrolls the page back to the top. This is achieved by detecting the scroll position with window.onscroll
and toggling the button's display accordingly. The button's click event triggers document.body.scrollTop
and document.documentElement.scrollTop
adjustments to ensure compatibility with various browsers. It enhances user experience by providing an easy way to navigate to the page's top without manually scrolling.
Bootstrap Scroll To Top Button using Javascript
xxxxxxxxxx
<style>
/* Custom CSS for the button */
#scrollToTopBtn {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
}
</style>
<div class="container p-3 shadow-sm">
<div style="height: 2000px;">
<h1>Bootstrap Scroll to Top of Page using Javascript</h1>
<p>Scroll down to see the Scroll to Top button.</p>
</div>
<!-- Scroll to Top Button -->
<button id="scrollToTopBtn" class="btn btn-primary" title="Go to top">
<i class="fas fa-arrow-up"></i> <!-- Font Awesome icon for "up" -->
</button>
</div>
<script>
// Get the button element
var scrollToTopBtn = document.getElementById("scrollToTopBtn");
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function () {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
scrollToTopBtn.style.display = "block";
} else {
scrollToTopBtn.style.display = "none";
}
};
// When the user clicks on the button, scroll to the top of the document
scrollToTopBtn.onclick = function () {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE, and Opera
};
</script>