document.addEventListener('DOMContentLoaded', function() {
var backToTopBtn = document.getElementById('backToTopBtn');
var footer = document.getElementById('yourFooterId');
window.onscroll = function() {
var scrollPosition = window.scrollY + window.innerHeight;
var footerPosition = footer.offsetTop;
if (scrollPosition > footerPosition) {
backToTopBtn.style.bottom = (scrollPosition - footerPosition + 20) + 'px';
} else {
backToTopBtn.style.bottom = '20px';
}
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
backToTopBtn.style.display = 'block';
} else {
backToTopBtn.style.display = 'none';
}
};
backToTopBtn.onclick = function() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
};
});