spring

javascript scroll to top

kimbs0301 2024. 10. 17. 22:37

.css

.scroll-to-top {
	position: fixed;
	right: 1rem;
	bottom: 1rem;
	display: none;
	width: 2.75rem;
	height: 2.75rem;
	text-align: center;
	color: #fff;
	background: rgba(90, 92, 105, 0.5);
	line-height: 46px;
}

.scroll-to-top:focus, .scroll-to-top:hover {
	color: white;
}

.scroll-to-top:hover {
	background: #5a5c69;
}

.scroll-to-top i {
	font-weight: 800;
}

 

.js

(function ($) {
	"use strict"; // Start of use strict

	// Scroll to top button appear
	$(document).on("scroll", function () {
		var scrollDistance = $(this).scrollTop();
		if (scrollDistance > 100) {
			$(".scroll-to-top").fadeIn();
		} else {
			$(".scroll-to-top").fadeOut();
		}
	});
	// Smooth scrolling using jQuery easing
	$(document).on("click", "a.scroll-to-top", function (e) {
		var $anchor = $(this);
		$("html, body").stop().animate({
			scrollTop: ($($anchor.attr("href")).offset().top)
		}, 200, "easeInOutExpo");
		e.preventDefault();
	});
})(jQuery);

 

.html

...
<body id="page-top">
    <div class="wrapper">
...
    </div>
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>
...
</body>
</html>