By Stephen Walker
Share
This creates a floating container. More to come.
.fixed--bottom{
position:fixed!important;
bottom:2rem;
transform: translateY(130px); /* Start off the screen */
transition: transform 0.75s ease-in-out; /* Smooth transition for sliding effect */
}
.fixed--bottom--left{
left:20px;
}
.fixed--bottom-center{
left:50%;
transform:translateX(-50%)
}
.show-floater .fixed--bottom{
transform: translateY(0);
z-index:10000;
}
.btn-tall{
height:100%
}
let lastScrollTop = 0; // Keep track of the last scroll position
window.addEventListener('scroll', function() {
let currentScroll = window.pageYOffset || document.documentElement.scrollTop;
const body = document.querySelector('body');
const floaterWrapper = document.querySelector('.fixed--bottom');
if (currentScroll > lastScrollTop) {
// Scrolling down
if (currentScroll > 100) {
// Slide up after scrolling down 100px
body.classList.add('show-floater');
}
} else {
// Scrolling up
// Slide off the screen
body.classList.remove('show-floater');
}
lastScrollTop = currentScroll <= 0 ? 0 : currentScroll; // For Mobile or negative scrolling
}, false);
Originally published on 4 weeks ago