有什么方法可以在滚动条顶部放置标记?

时间:2019-08-22 15:13:19

标签: javascript html css scrollbar

类似于许多IDE和文本编辑器(例如https://github.com/surdu/scroll-marker)中的功能,我希望能够在滚动条上显示overflow: scroll元素的小标记。

我认为可以通过在可滚动元素上放置pointer-events: none元素作为覆盖物并在其上放置绝对定位的元素来完成此操作,但是考虑到滚动条具有平台/浏览器,因此正确定位该位置将非常棘手-特定的宽度,内边距,小块大小以及类似的内容。此外,这似乎是一个很棘手的解决方案。

有指针吗?

1 个答案:

答案 0 :(得分:0)

您在这里。这是一个巨大的挑战。它不是完美的,但我希望它能为您指明前进的方向。
因此,首先进行简要说明。

有一个尺寸固定的容器。其中有2个元素:.inner-container用于文本,而.scroll-marker将在滚动条上带有标记。

文本是<p>标签,而突出显示的文本是具有<span>.red.yellow类的blue标签。这些类用于了解每个标记的颜色。每个标记都是循环创建的,并附加在.scroll-marker上,它们的位置绝对正确。为了使滚动定位正确进行,需要进行三个规则计算。

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>