Progress bar indicating how far users have scrolled within a specific element

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?

Answer №1

The issue arises from setting onscroll to window instead of

document.getElementById("info")
. Take a look at the revised example below:

function myFunction() {
    const winScroll = document.getElementById("info").scrollTop;
    const height = document.getElementById("info").scrollHeight - document.documentElement.clientHeight;
    const scrolled = (winScroll / height) * 100;
    document.getElementById("myBar").style.width = scrolled + "%";
  }

 document.getElementById("info").onscroll = function() {myFunction()};
.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%;
}

#info {
  width: 256px;
  height: 256px;
  overflow-y: auto;
  background-color: red;
}
<div name="header" class="header">
    <div name="progress-container" class="progress-container">
        <div name="myBar" class="progress-bar" id="myBar"></div>
    </div>
</div>

<div id="info">
  <p>SOME TEXT</p>
  <p>SOME TEXT</p>
  <p>SOME TEXT</p>
  <p>SOME TEXT</p>
  ...
  <p>SOME TEXT</p>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to illustrating the connections between origin and destination nations utilizing IP addresses

My goal is to create an interactive world map with a clickable marker for each country. When a user clicks on a source country's marker, I want to display interactions with other countries in an aggregated manner. While I have successfully drawn the w ...

Having trouble accessing the menu in the dropdown div when clicking back?

I am working on creating a menu that drops down from the top of the page using JQuery. I have everything set up with the code below: <div id="menu"> <div id="menucontentwrapper"> <div id="menucontent"></div> </di ...

What is the best way to transfer a JavaScript object with string values containing quotes from node.js to the browser?

I have a node/express application and need to send a JavaScript object to the browser. Currently, I achieve this by using JSON.stringify on the object and then embedding it into the HTML: In my Node.js/Express code: var myObject = /* fetched from databas ...

A straightforward method of transmitting data from JavaScript to the Python back-end within a Streamlit application

Utilizing the st.components.v1.iframe, I integrated an authentication system that sends a token back to the parent when a user is authenticated. The iframe content is as follows: <script src="remote/auth.js"></script> <scri ...

Transform your traditional sidebar into a sleek icon sidebar with Semantic UI

I am working on customizing the semantic-ui sidebar. My goal is to have it minimize to a labeled icon when the toggle button is clicked. However, I am having trouble with the animation and getting the content to be pulled when I minimize it to the labeled ...

Tips for creating a responsive text in a label using Tailwind CSS

Today I ran into an issue with the text alignment in my password label when changing the resolution of my device. Can someone please help me fix this? Here is my code: <div class="flex"> <BreezeLabel for="password" ...

Using the same Angular component with varying input values

Recently, I encountered a puzzling issue that has left me unsure of where to even start investigating the possible causes. I am currently utilizing a library called angular-stl-model-viewer, which is based on Three.js. The problem arises when I call a comp ...

Removing a particular value from a cookie using jquery

I'm currently in the process of creating a shopping cart system that allows users to add or remove products from their basket. For each product, I am storing two pieces of information in the product cookie: the product barcode and price. This is wha ...

Fetch image from database using Ajax when image name is clicked in CodeIgniter

I am currently working on creating a gallery using CodeIgniter. I have successfully retrieved a list of image names from the database and displayed them on the view page. Now, I would like to implement a feature where users can click on an image name and r ...

Spotlight the content of the file obtained from GitHub

I'm having trouble with syntax highlighting for code fetched from GitHub. fetch("https://raw.githubusercontent.com/Ayanmullick/test/master/AutomationAcc/test1.ps1") .then(response => response.text()) .then(data => document.getElem ...

Introducing the latest feature in Vue.js 3.2: script setup with dynamic $

I am attempting to generate a dynamic summary. <template> <ul> <li v-for="(category, i) in categories" :key="i" class="cursor-pointer text-capitalize q-mb-sm" @click="scrollTo('category&apo ...

The problem with utilizing the Node `util.inherits` method

I have encountered an issue with a 'this problem' in a Node server. It seems that replacing worker.stuff with worker.stuff.bind(worker) is necessary for it to function correctly. Is there a way to incorporate the bind method into the Worker Clas ...

AJAX request stops functioning once the page is reloaded

As a beginner in JavaScript, I am facing an issue with my AJAX call. I have set up the call to process a back-end function when a button is clicked and expect to receive a response once the function is completed. However, whenever I refresh the page whil ...

Guide on incorporating a displacement map into a vertex shader in ThreeJS

I am a beginner in ThreeJS and I am attempting to create a asteroids-like scene with textures made of particles, similar to what is showcased on this website . However, my attempt to apply a displacement map to a points material was unsuccessful. How can ...

Class-validator does not have any associated metadata

Struggling with implementing a ValidationPipe in my code, I consistently encounter the warning "No metadata found. There is more than one class-validator version installed probably. You need to flatten your dependencies" when sending a request. The struct ...

How can we identify context menu events triggered by touch actions?

In my application, I have implemented drag-and-drop functionality for elements within an SVG element using d3-drag and d3-zoom. However, on touch-enabled devices such as IE11, Edge, and Firefox, a context menu pops up after a long press, which interferes w ...

Creating a Progress Bar in Javascript to Compare Three Percentage Values

Trying to solve a persistent issue. In my Symfony application with Bootstrap, I created a ProgressBar. The code snippet below displays the bar: <link href="https://bootswatch.com/5/sketchy/bootstrap.min.css" rel="stylesheet"/> <div class="prog ...

Using only CSS to style a blockquote

Currently attempting to customize the appearance of a blockquote to match this design: However, it currently appears like this: (it should not have any white spaces at the beginning) The code I'm using <blockquote><p>Lorem ipsum...&l ...

Exploring ways to display dynamic content within AngularJs Tabs

I need help figuring out how to display unique data dynamically for each tab within a table with tabs. Can anyone assist me with this? https://i.stack.imgur.com/63H3L.png Below is the code snippet for the tabs: <md-tab ng-repe ...

Adding over 20,000 rows to a table can be time-consuming, causing the page to become unresponsive once the process is complete

Looking at my table structure, it appears as follows: <tr ng-repeat="friend in friends | orderBy:predicate:reverse"> <td>{{friend.name}}</td> <td>{{friend.phone}}</td> <td>{{f ...