Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
How can I add 1% to the width of a div inside of a loop using only JS?

I've been trying to add 1% to the width of the div using JS. I want to make it so that the bar's width increases in a smooth way ( I thought about using animations but it didn't work cause I need to specify conditions).

window.onload = function(){
    for (let i = 0; i < 5; i++) {
        const bar = document.querySelectorAll(".child-bar")[i];
        
        for (let j = 0; j < 82; j++) {
            //alert("j" + j);
            console.log("bar width: "+  bar.style.width)
            bar.style.width += '1%';
        }
    }   
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="skill">
   <label for="HTML">HTML</label>
     <div class="parent-bar">
        <span class="child-bar"></span>
          <h4>82%</h4>
     </div>
</div>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Maybe this example will help you?

window.onload = function() {
  document.querySelectorAll(".parent-bar").forEach((el) => {
    const barNode = el.querySelector(".child-bar");
    const valueNode = el.querySelector("h4");
    const max = 82;
    const duration = 100;
    let value = 0;

    const tick = () => {
      barNode.style.width = `${value}%`;
      valueNode.innerHTML = `${value}%`;
      if (value++ < max) setTimeout(tick, duration);
    }
    tick();
  })

}
.child-bar {
  display: block;
  background: black;
  height: 1rem;
  width: 0;
  transition: 0.1s 0s linear;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="skill">
  <label for="HTML">HTML</label>
  <div class="parent-bar">
    <span class="child-bar"></span>
    <h4></h4>
  </div>
  <div class="parent-bar">
    <span class="child-bar"></span>
    <h4></h4>
  </div>
  <div class="parent-bar">
    <span class="child-bar"></span>
    <h4></h4>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Here's a solution with verbose code and commentary for clarity, using setInterval.
(Note that span elements have display: inline by default, which is why setting their width has no effect.)

// Sets options and calls main function
const myOptions = {
  MAX_WIDTH_PERCENT: 82,
  FRAME_COUNT: 100,
  FRAME_DURATION: 20
};
animateBars(myOptions);


// Defines main function
function animateBars(options){
  const  
    // Destructures options object to make local variables
    { MAX_WIDTH_PERCENT, FRAME_COUNT, FRAME_DURATION } = options,

    // Defines function to increment width
    increment = (value) => value += MAX_WIDTH_PERCENT / FRAME_COUNT,

    // Defines function to update bar (after incrementing width)
    incrementAndupdateBar = (bar, oldWidth, timerId) => {
      newWidth = Math.min(increment(oldWidth), MAX_WIDTH_PERCENT);
      bar.style.width = newWidth + "%";
      bar.nextElementSibling.textContent = Math.floor(newWidth) + "%"; // (For demo)

      // Stops repeating the setInterval's function
      if(newWidth == MAX_WIDTH_PERCENT){
        clearInterval(timerId);
      }
      return newWidth; // Returns updated value
    };


  // Loops through bars
  for(let bar of document.querySelectorAll(".child-bar")){

    // Repeatedly updates current bar, keeping track of width as we go
    let
      width = 0, // Initializes width for the current bar
      timerId; // ID for use by setInterval & clearInterval

    timerId = setInterval( // (Returns an ID for later use)

      // Takes 2 args: a func and a duration (in ms) to delay inbetween
      function(){width = incrementAndupdateBar(bar, width, timerId);},
      FRAME_DURATION
    );
  }
}
.parent-bar{ width: 300px; border: 1px dotted grey; }
/* spans ignore "width" by default; "inline-block" solves this */
.child-bar{ display: inline-block; height: 1em; background-color: lightgreen; }
h4{ margin: 0; text-align: center; }
<div class="parent-bar">
  <span class="child-bar"></span>
  <h4></h4>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda