var container = document.querySelector('.container');
var containerInner = document.querySelector('.container-inner');
var containerHeight = container.offsetHeight;
var containerScrollHeight = containerInner.scrollHeight;
var scrollMarker = document.querySelector('.scroll-marker');
var colorfulStuff = document.querySelectorAll('.container-inner span'); // colorful spans from text
colorfulStuff.forEach(function (span) { // loop to create each marker
var spanTop = span.offsetTop;
var spanBottom = spanTop + span.offsetHeight;
var markerTop = Math.ceil(spanTop * containerHeight / containerScrollHeight);
var markerBottom = Math.ceil(spanBottom * containerHeight / containerScrollHeight);
if (span.className === "red") { // choose the correct color
var markerColor = '#f65e5a';
} else if (span.className === "yellow") {
var markerColor = '#fec740';
} else if (span.className === "blue") {
var markerColor = '#2985d0';
}
var markerElement = document.createElement("span"); // create the marker, set color and position and put it there
markerElement.style.backgroundColor = markerColor;
markerElement.style.top = markerTop + "px"
markerElement.style.height = (markerBottom - markerTop) + "px"
scrollMarker.appendChild(markerElement);
})
.container {
background: #2d3744;
border-radius: 4px;
position: relative;
margin: 30px auto;
height: 320px;
width: 300px;
box-shadow: 2px 2px 5px 0 rgba(0, 0, 0, 0.65);
}
.container-inner {
padding: 20px 30px;
height: calc(100% - 40px);
overflow-y: auto;
overflow-x: hidden;
}
p {
color: white;
margin: 12px 0;
font-size: 18px;
font-family: sans-serif;
letter-spacing: 0.44px;
}
.red {
color: #f65e5a;
}
.yellow {
color: #fec740;
}
.blue {
color: #2985d0;
}
.scroll-marker {
position: absolute;
height: 100%;
width: 12px;
pointer-events: none;
top: 0;
right: 0;
}
.scroll-marker span {
/* markers */
position: absolute;
width: 100%;
}
/* scrollbar style */
.container-inner::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
background-color: #2d3744;
}
.container-inner::-webkit-scrollbar {
width: 12px;
background-color: #2d3744;
}
.container-inner::-webkit-scrollbar-thumb {
border-radius: 10px;
-webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);
background-color: #4c5f74;
}
<div class="container">
<div class="container-inner">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore asperiores <span class="yellow">provident magnam impedit deleniti modi, culpa perspiciatis ex illum iusto vel nulla expedita itaque temporibus</span> voluptate non magni. Voluptatem, itaque.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore asperiores provident <span class="blue">magnam impedit deleniti modi</span>, culpa perspiciatis ex illum iusto vel nulla expedita itaque temporibus voluptate non magni. Voluptatem, itaque.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. <span class="yellow">Inventore asperiores provident</span> magnam impedit deleniti modi, culpa perspiciatis ex illum iusto vel nulla expedita itaque temporibus voluptate non magni. Voluptatem, itaque.</p>
<p><span class="red">Lorem ipsum</span> dolor sit amet consectetur adipisicing elit. Inventore asperiores provident magnam impedit deleniti modi, culpa <span class="red">perspiciatis ex illum iusto vel nulla expedita itaque temporibus voluptate non magni.</span> Voluptatem, itaque.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore asperiores provident magnam impedit deleniti modi, culpa perspiciatis ex illum iusto vel nulla expedita itaque temporibus voluptate non magni. Voluptatem, itaque.</p>
<p><span class="yellow">Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore asperiores provident magnam impedit deleniti modi, culpa perspiciatis ex illum iusto vel nulla expedita itaque temporibus</span> voluptate non magni. Voluptatem, itaque.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore asperiores provident magnam impedit deleniti modi, culpa perspiciatis ex illum iusto vel nulla expedita itaque temporibus voluptate non magni. <span class="blue">Voluptatem, itaque.</span></p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore asperiores provident magnam impedit deleniti modi, culpa perspiciatis ex illum iusto vel nulla expedita itaque temporibus voluptate non magni. Voluptatem, itaque.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore asperiores provident magnam impedit deleniti modi, culpa perspiciatis ex illum iusto vel nulla expedita itaque temporibus voluptate non magni. Voluptatem, itaque.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore asperiores <span class="yellow">provident magnam</span> impedit deleniti modi, culpa <span class="blue">perspiciatis ex illum iusto vel nulla expedita itaque</span> temporibus voluptate non magni. Voluptatem, itaque.</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Inventore asperiores provident magnam impedit deleniti modi, culpa perspiciatis <span class="red">ex illum</span> iusto vel nulla expedita itaque temporibus voluptate non magni. Voluptatem, itaque.</p>
</div>
<div class="scroll-marker"></div>
</div>