I'm currently working on implementing a scroll progress indicator for a div
element. While the indicator is functional, it seems to only respond to window scrolls and ignores any overflow scrolling within the div
.
function handleScroll() {
var winScroll = document.getElementById("info").scrollTop;
var height = document.getElementById("info").scrollHeight - document.documentElement.clientHeight;
var scrolled = (winScroll / height) * 100;
document.getElementById("myBar").style.width = scrolled + "%";
}
window.onscroll = function() {
handleScroll()
};
.header {
position: fixed;
top: auto;
z-index: 1;
width: 83.5%;
background-color: #f1f1f1;
}
.progress-container {
width: 100%;
height: 8px;
background: #ccc;
}
.progress-bar {
height: 8px;
background: #04AA6D;
width: 0%;
}
<div name="header" class="header">
<div name="progress-container" class="progress-container">
<div name="myBar" class="progress-bar" id="myBar"></div>
</div>
</div>
It appears that my implementation may have an issue or perhaps I need to replace window.onscroll
with an alternative method. Ideally, I'd like to avoid using jQuery for this. Any suggestions?