JSFiddle - React, Tailwind, and code Playground
by adamschwartz
HTML
<div id="scrollbox">
<div id="box"></div>
<div id="heightbox"></div>
<div id="output"></div>
</div>
CSS
</style>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html, body {
overflow: hidden;
height: 100%;
width: 100%;
}
body {
margin: 0
}
#box {
background: #000;
height: 50px;
width: 50px;
position: fixed;
top: -25px;
bottom: 0;
-webkit-transform: translate3d(0, 0, 0)
}
#heightbox {
display: block;
height: 3000px;
width: 100%;
background: -webkit-linear-gradient(#fff, #ccc);
background-size: 100% 50px
}
output {
position: fixed;
margin: auto;
display: inline-block;
white-space: nowrap;
padding: 20px;
top: 0;
bottom: 0;
left: 100px;
right: 100px;
height: 30px;
font-size: 30px;
line-height: 30px;
max-width: 500px;
background: #000;
color: #fff;
text-align: center;
}
#scrollbox {
overflow: auto;
height: 100%;
width: 100%;
-webkit-overflow-scrolling: touch;
}
JavaScript
var reposition, interval, timeout, height;
reposition = function() {
var scrollTop, windowHeight, position;
scrollTop = scrollbox.scrollTop;
windowHeight = window.innerHeight;
scrollHeight = heightbox.clientHeight;
console.log(scrollTop, windowHeight, scrollHeight);
position = scrollTop * windowHeight / (scrollHeight - windowHeight);
position = Math.floor(position);
box.style.webkitTransform = 'translate3d(0, ' + position + 'px, 0)';
output.innerHTML = position;
};
window.addEventListener('scroll', reposition);
scrollbox.addEventListener('scroll', reposition);
window.addEventListener('touchmove', reposition);
scrollbox.addEventListener('touchmove', reposition);
document.body.addEventListener('touchstart', function(e){
clearTimeout(timeout);
interval = setInterval(reposition);
});
document.body.addEventListener('touchend', function(e){
timeout = setTimeout(function(){
clearInterval(interval);
}, 5000);
});