Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to place markers on top of scrollbars?

Similar to features found in many IDEs and text editors (such as https://github.com/surdu/scroll-marker), I want to be able to show small markers on the scrollbar (of a overflow: scroll element).

I think this could be done by putting a pointer-events: none element as an overlay on the scrollable element and putting absolute-positioned elements on it, but it would be tricky getting the position right considering the scrollbar has platform/browser-specific width, padding, nub size, and things like that. Besides, it feels like a rather hacky solution.

Any pointers?

like image 939
Kyuuhachi Avatar asked Oct 21 '25 10:10

Kyuuhachi


1 Answers

Here you go. It was a great challenge. It's not perfect, but I hope it shows you a way forward.
So, first a brief explanation.

There is a container with fixed dimensions. There are 2 elements inside it: .inner-container for the text and the .scroll-marker that will have the marks over the scrollbar.

The text are <p> tags, and the highlighted text are <span> tags with .red, .yellow or blue classes. These classes are being used to know the color for each mark. Each mark is created in a loop and appended on .scroll-marker, they are absolutely positioned. A rule calculation of three is done to make the scroll positioning happen correctly.

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>
like image 150
Azametzin Avatar answered Oct 23 '25 22:10

Azametzin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!