I made a custom scrollbar and the only problem is that whenever I zoom in or zoom out (Ctrl + mousewheel), the width (width: 10px) changes as it is in pixels. If I change it to width: 1vw, then it works fine with zoom but on window resize it changes again.
Making width: 1vh, also changes the size of scrollbar when resizing window vertically.
So, how to make this scrollbar's size be fixed and independent of zoom/window resizing?
CSS Code
#scroll-bar {
position: fixed;
width: 1vh;
height: 100%;
top: 0;
right: 0;
background: black;
}
Javascript Code
<script>
let bar = document.getElementById('scroll-bar');
let totalHeight = document.body.scrollHeight - window.innerHeight;
window.onscroll = function () {
let bar_height = (window.pageYOffset/totalHeight)*100;
bar.style.height = bar_height+'%';
}
</script>