HTML代码:
<button id="scrollToTopBtn" class="btn btn-primary">
<i class="fas fa-arrow-up"></i>
</button>
JS代码:
$(function () {
const scrollToTopBtn = $('#scrollToTopBtn');
$(window).on('scroll', function () {
if ($(this).scrollTop() > 0) {
if (!scrollToTopBtn.is(':visible')) {
scrollToTopBtn.fadeIn();
}
}
});
scrollToTopBtn.on('click', function () {
$('html, body').animate({ scrollTop: 0 }, 'slow', function () {
scrollToTopBtn.fadeOut();
});
});
});
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容