I've been working on adding a back-to-top button to my website using JavaScript in Bootstrap 4 with the Font Awesome icon set. It was functioning perfectly until I made some changes to the site, and now it's not working anymore. I'm pretty new to JavaScript and can't figure out what went wrong...
This is the code I have:
$(document).ready(function() {
$(window).scroll(function() {
if ($(this).scrollTop() > 50) {
$('#back-to-top').fadeIn();
} else {
$('#back-to-top').fadeOut();
}
});
$('#back-to-top').click(function() {
$('body,html').animate({
scrollTop: 0
}, 400);
return false;
});
});
.back-to-top {
position: fixed;
bottom: 25px;
right: 25px;
display: none;
}
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="height:100rem;">test</div>
<a id="back-to-top" href="#" class="mr-m2b btn btn-primary btn-lg back-to-top" role="button">TOP<i class="fas fa-chevron-up"></i></a>
It used to work flawlessly until something caused it to stop working. The JavaScript seems to be the issue as it's not doing much, but I'm unsure why. Why did it work before without any changes to the function?!
Edit: I recently removed a custom scrollbar
body::-webkit-scrollbar {
width: 1em;
}
body::-webkit-scrollbar-track {
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
}
body::-webkit-scrollbar-thumb {
background-color: #A1394F;
outline: 1px solid slategrey;
}
Adding the deleted scrollbar back didn't solve the issue either. The same JavaScript code works fine here, but not on my HTML page... What am I overlooking?