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

251
Visualizações
Deleting 5% of width every time the button is clicked JS

I don't know how to write a simple event listener to delete 5% od elements width every time the button is clicked. From 100% to 0%.

substracktBtn.addEventListener('click', function(){
    container.style.width = "calc(" + container.style.width + " - 5%)";
})

In this code i got no errors but the div's width is still 100% after the clicks.

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

0

This is your solution:

  • It will remove 5% of the beginning width each time till you reach 0%. With the other solution you will never get to 0% because you always take the 5% of the current width.

const container = document.querySelector('div')
const button = document.querySelector('button')
const subtractPerClick = container.offsetWidth * 0.05

button.addEventListener('click', () => {
  if (container.offsetWidth - subtractPerClick > 0)
    container.style.width = container.offsetWidth - subtractPerClick + 'px'
  else
    container.style.width = '0px'
})
div {
  height: 100px;
  width: 300px;
  background-color: darkblue;
}
<div></div>
<button>Substract</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

For removing 5% of the current width. This converges!

const container = document.getElementById('container');
const button = document.getElementById('button');

button.addEventListener('click', () => {
  container.style.width = container.offsetWidth * 0.95 + 'px'
})
#container {
  background-color: red;
  width: 200px;
  height: 100px;
}
<div id="container"></div>

<button id="button">Substract</button>

For removing 5% of the starting width

const container = document.getElementById('container');
const button = document.getElementById('button');

let startWidth5Percent = container.offsetWidth * 0.05;

button.addEventListener('click', () => {
   container.style.width = container.offsetWidth - startWidth5Percent + 'px';
})
#container {
  background-color: red;
  width: 200px;
  height: 100px;
}
<div id="container"></div>

<button id="button">Substract</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

How about just:

substracktBtn.addEventListener('click', function(){
    container.offsetWidth = container.offsetWidth * 0.95
});

Note: untested, not sure if JS requires the 'px' added at the end, but you get the gist.

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