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

88
Visualizações
Prevent style attributes from changing back to default as others are change

This snippet of code changes the brightness and contrast of an image. However, when setting the contrast to 150% for example and then attempting to change the brightness the contrast value defaults back to 100%. How can I prevent these style attributes from changing back to their default value after being changed?

<p>
   Contrast Bar
</p>
<input id='contrast_bar' type="range" min="50" max="150" value="100">
<p>
   Brightness Bar
</p>
<input id='brightness_bar' type="range" min="50" max="150" value="100">
<BR><BR>
<img id='img_selection' src="photo.jpg">
<script>
   let contrast_bar = document.querySelector('[id=contrast_bar]')
   let brightness_bar = document.querySelector('[id=brightness_bar]')
   let img = document.querySelector('#img_selection')

   contrast_bar.addEventListener('input', con_bar => {
     img.style.filter = 'contrast(' + con_bar.target.value + '%)'
   })

   brightness_bar.addEventListener('input', bright_bar => {
     img.style.filter = 'brightness(' + bright_bar.target.value + '%)'
   })
</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is because you overwrite the previous setting of filter when you change each range slider, therefore only ever one filter type is updated.

To fix the problem create a function which reads the values from both range inputs and creates a single string with both brightness and contrast values, and call this function any time either range is changed.

let contrast_bar = document.querySelector('#contrast_bar');
let brightness_bar = document.querySelector('#brightness_bar');
let img = document.querySelector('#img_selection');
let updateFilter = () => img.style.filter = `contrast(${contrast_bar.value}%) brightness(${brightness_bar.value}%)`;

contrast_bar.addEventListener('input', updateFilter);
brightness_bar.addEventListener('input', updateFilter);
img { width: 300px; }
<p>Contrast Bar</p>
<input id="contrast_bar" type="range" min="50" max="150" value="100" />

<p>Brightness Bar</p>
<input id="brightness_bar" type="range" min="50" max="150" value="100" /><br /><br />

<img id="img_selection" src="https://i.imgur.com/WkomVeG.jpg" />

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