What is the best way to reposition the origin to a suitable location using JavaScript?

I am currently struggling with a challenge. My goal is to make the dot on the progress bar reach specific positions when the value is 10 or 20. However, I am facing difficulties in achieving this due to gaps between the segments of the progress bar. I would greatly appreciate any help or advice on how to overcome this obstacle. Thank you.

// To determine dot position based on progress value
var progress = 10;
if(progress == 10){
    progress--
}else if(progress == 20){
    progress-- 
}else if(progress == 30){
    progress--
}
//Calculate where the dot origin should appea
var dot = document.getElementById("dot");
var dotPosition = (progress / 30) * 100;
dot.style.left = `calc(${dotPosition}% - 20px)`;
body {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

#progress-bar-container {
  width: 600px;
  height: 5px;
  background-color: #fff;
  position: relative;
  display: flex;
  flex-wrap: nowrap;
}

.progress-bar {
  width: 33.33%;
  height: 10px;
  background-color: #fff;
  margin-right: 8px;
  border-radius: 20px;
}

#progress-bar-1 {
  background-color: #ddd;
}

#progress-bar-2 {
  background-color: #ddd;
}

#progress-bar-3 {
  background-color: #ddd;
  margin-right: 0;
}

#dot {
  width: 20px;
  height: 10px;
  border-radius: 5px;
  background-color: orange;
  position: absolute;
  top: 0px;
  left: 0;
  transition: left 0.2s ease-out;
  overflow: hidden;
}
<div id="progress-bar-container">
  <div class="progress-bar" id="progress-bar-1"></div>
  <div class="progress-bar" id="progress-bar-2"></div>
  <div class="progress-bar" id="progress-bar-3"></div>
  <div id="dot"></div>
</div>
https://i.sstatic.net/JIooK.png

Answer №1

In order to determine the position of the dot relative to the progress bar, you can use the absolute position of the progress bar on the screen obtained with element.getBoundingClientRect(). Essentially, you would map the dot's position based on the percentage of completion of the progress bars. For instance, if the value falls between 33.3% and 66.6%, the dot should be positioned between the starting coordinates of the second progress bar and those coordinates plus the width of the progress bar. Hopefully, this explanation clarifies things for you.

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

Top Tip for conditionally rendering a styled component inside or outside a wrapper, depending on the screen width

Can anyone help me with this coding question? I'm currently trying to determine the most efficient way to conditionally place the code inside or outside of the wrapper. However, I am unsure what the best practice for this would be. This process seems ...

Using Puppeteer to cycle through a CSV file and take a screenshot for each individual row?

I am looking to automate screenshotting URLs from a CSV file using puppeteer, but the current code execution is slow as each request waits for the previous one to finish. const csv = require('csv-parser'); const fs = require('fs'); con ...

Using the includes method with a switch statement in Javascript

Hey there, I'm facing a bit of a challenge here. It seems like what I'm attempting to do might be more complex than I initially thought or my brain is just not cooperating. I'm currently working on using a switch statement to verify if a str ...

What could be causing the flex item to be wider than its container?

One issue that can arise when utilizing flexbox is the scenario where, as demonstrated below, the content exceeds the width of the viewport causing the flexbox items to become wider than their container. What are the factors contributing to this situat ...

Sort Through Hexadecimal Color Codes

I am faced with a challenge of sorting through a vast array of hex colors (over 6500) and I need to eliminate the ones that are excessively dark or overly bright. Is there a distinguishing feature in the hex codes of dark and bright colors that can help m ...

Tips for maximizing the benefits of debounce/throttle and having a truly dynamic experience

Attempting to implement a similar concept in Vue: props(){ debouncing: {type: Number, default: 0} }, methods: { clicked: _.debounce(function() { this.$emit('click'); }, this.debouncing), } Unfortunately, the code breaks when ...

Having trouble getting my Blender model to render accurately in three.js

Currently, I am attempting to create a basic gear animation for my website using three.js. Initially, I crafted a gear mesh in Blender with a splendid, shiny bronze material, and exported it to three.js json format using the blender exporter and the code p ...

Adding a Header Image to the Top of All Pages with CSS

As I dive into the world of website creation, I've encountered a challenge that has me stumped. My goal is to have a banner displayed at the top of each webpage on my site, and I'm unsure if this can be achieved using CSS. While I've success ...

What is the best way to retrieve all the listed TV and film ratings in descending order? Using Django

Our Goal I aim to organize movies based on their star ratings and filter out those with ratings lower than a specified threshold. 2. Retrieve the IDs of Movies and TV shows mentioned in the view, fetch the data and score through URLs one by one. 3. Presen ...

Storing JSON data in an array is a simple process that involves converting

Currently, I am working on implementing dependent dropdowns using JavaScript. The dropdowns include country, state, and city, with the data sourced from the JSON data provided below. My goal is to organize the data in such a way that countries are stored ...

Tips for minimizing excess white space in Shiny applications

Encountering an issue with plot output in Shiny. When using standard ggplots, the output is correct without any extra white margins. However, when utilizing the "ggmap" package (which also produces ggplot-type outputs), white margins suddenly appear. (Ple ...

Implementing Text Box Control Validation within a Gridview using UpdatePanel in c# ASP.Net

In my gridview, one of the columns contains a Text Box Control. I am looking to validate the text entered by users as alphanumeric characters and spaces only. Allowed characters are: a-z, A-Z, 0-9, and space. I want to perform this validation using Java ...

The issue with CSS3 rotate3d rotation

I'm facing an issue with the rotate3d function. Take a look at this Fiddle: https://jsfiddle.net/c3snpkpr/ The problem arises when hovering over the <div> from the left or right - sometimes it rotates properly, and other times it bugs out by r ...

Order of flexbox items when placed within separate divs?

Looking to rearrange the order of items using flexbox, but hitting a roadblock because one of the items I want to reorder is in a different div and not a direct child of the same parent as the other items. <div class="wrapper"> <div class="some ...

What is the process for printing a page in plain text format on a matrix-dot printer?

I've come across many similar questions on SO about this issue, and I've tried various solutions without success. I'm using an Epson LX300+II printer and I want to print pages from my web application in a plain format, like how it would look ...

Is there a way to utilize DOM in javascript to retrieve list values?

I am looking to retrieve an array or simple string values containing the items in an <ul> element. Below is my code snippet: <!DOCTYPE html> <html> <head> </head> <body> <h2>Unordered List with Disc Bullets</h ...

Achieve Vertical and Horizontal Alignment of a div within another div using the :after selector

I currently have this HTML and CSS code: html { overflow-x: hidden; overflow-y: hidden; } body { margin: 0; } .triangle-down { width: 90%; height: auto; padding-left: 50%; padding-top: 50%; overflow: hidden; } .triangle-down:after { co ...

Ways to implement div on hover in my table's td cells

Here is the HTML table code I have: <table border='1px'> <tr> <td id='td'>first</td> <td>last</td> <tr> <td id='td'>newFirst</td> <td>newSecond</td> </t ...

Ways to inform ngForm that the nested form has been submitted

What I understand Within Angular 1.3 and higher, I can utilize $submitted to determine if the specific form has been submitted successfully. This function works flawlessly when working with a form structured in the following manner: <form name="myForm ...

Answer for background picture when reducing magnification

Currently, I am facing the challenge of converting a PSD template to HTML. Specifically, I need guidance on how to handle the background food images (highlighted in red in the image) when zooming out the browser. The total width is 1300px with a container ...