JSFiddle - React, Tailwind, and code Playground
HTML
<div id="scroll"></div>
<div id="bigBlock"></div>
CSS
body {
padding:0px;
margin:0px;
}
#scroll {
background:green;
height:50px;
width:50px;
position:fixed;
right:0px;
}
#bigBlock {
height:5000px;
}
JavaScript
$(document).scroll(function () {
var $document = $(this);
var $block = $('#scroll');
var blockHeight = $block.height();
var bodyHeight = $document.height();
var viewportHeight = $(window).height();
var scrollPosition = $document.scrollTop();
var blockPosition = (scrollPosition / (bodyHeight - viewportHeight) * (viewportHeight-blockHeight));
$block.css('top', Math.round(blockPosition) + 'px');
});