JSFiddle - React, Tailwind, and code Playground

by Sergey Aleshin

HTML

<div class="wrapper">
    <div class="sidebar" id="sidebar">
        Sidebar
    </div>
    <div class="content">
        <div class="block">
            Block
        </div>
        <div class="block">
            Block
        </div>
    </div>
</div>

CSS

body {
    color: #FFF;
}
.sidebar {
    float: left;
    width: 100px;
    height: 40px;
    margin-top: 20px;
    
    background-color: #999;
}

.content {
    margin-left: 110px;
    padding-top: 20px;
}

.block {
    height: 500px;
    margin-bottom: 10px;
    
    background-color: #333;
}
.block:last-child {
    margin-bottom: 0px;
}

JavaScript

var sidebar = document.getElementById("sidebar");
window.onscroll = function () { 
	var scroll = window.pageYOffset || document.documentElement.scrollTop;
	if (scroll >= 20) 
			sidebar.style.marginTop = scroll +"px";
	else 
		sidebar.style.marginTop = "";
}