<div class="fixedDiv">123213</div>
<div class="fixedDiv fixedDiv_second">312312</div>
body {
height: 10000px;
}
#fixedDiv {
height: 100px;
}
.fixedDiv_second {
font-size: 30px;
}
#red {
background: red;
}
#blue {
background: blue;
}
.fixed-top {
position: fixed;
top: 0;
}
.fixed-bottom {
position: fixed;
bottom: 0;
background: green;
}
var oldScrollY = 0;
var divs = [].slice.call(document.querySelectorAll(".fixedDiv"));
window.onscroll = function() {
var scrolled = window.pageYOffset || document.documentElement.scrollTop;
var dY = scrolled - oldScrollY;
if ( dY > 0 ) {
divs.forEach(function (div) {
div.classList.remove('fixed-top');
div.classList.add('fixed');
div.classList.add('fixed-bottom');
});
} else {
divs.forEach(function (div) {
console.log(div);
div.classList.remove('fixed-bottom');
div.classList.add('fixed');
div.classList.add('fixed-top');
});
}
oldScrollY = scrolled;
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.