Skip to content

Instantly share code, notes, and snippets.

@oleksapro
Last active March 29, 2018 07:29
Show Gist options
  • Save oleksapro/c3b2cef9506010ee22e64396a30fe6e7 to your computer and use it in GitHub Desktop.
Save oleksapro/c3b2cef9506010ee22e64396a30fe6e7 to your computer and use it in GitHub Desktop.
if (window.innerWidth > 640) {
var floatParentNode = document.querySelector('.js-parent');
var floatNode = document.querySelector('.js-float');
var valScrollForFixed = floatParentNode.getBoundingClientRect().top + window.pageYOffset - document.querySelector('.js-header').offsetHeight;
window.addEventListener('scroll', function () {
if (window.innerHeight > floatNode.offsetHeight) {
var windowScrollTop = window.pageYOffset;
var footerTop = document.querySelector('.js-footer').getBoundingClientRect().top + windowScrollTop;
var footerHeight = document.querySelector('.js-footer').clientHeight;
var valScrollForAbs = windowScrollTop + floatNode.offsetHeight + document.querySelector('.js-header').offsetHeight + 20;
if (valScrollForAbs >= footerTop) {
// floatNode.classList.remove('fixed');
floatNode.classList.add('fixed-footer');
floatNode.style.bottom = -(document.body.clientHeight - windowScrollTop - window.innerHeight - footerHeight - 20) + 'px';
} else {
floatNode.classList.remove('fixed-footer');
floatNode.style.bottom = '';
if (windowScrollTop >= valScrollForFixed) {
floatNode.classList.add('fixed');
} else {
floatNode.classList.remove('fixed');
}
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment