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

265
Visualizações
VueJs switch buttons color

Im new in vuejs and i need a example how to switch color between two buttons when click then. For example:when I click the white button it turns black and the other one turns white, and vice versa

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

0

In your template:

<div class="button-wrapper" :class="{'one-is-active': state.oneIsActive}">
  <div class="button one" @click="toggleButton()" />
  <div class="button two" @click="toggleButton()" />
</div>

In your setup (This is just a suggestion, you could also use a ref or something else; may differ by version.):

const state = reactive({
  oneIsActive: true,
});

function toggleButton() {
  state.oneIsActive = !state.oneIsActive;
}

Your SCSS (may differ a bit in pure css or sass):

.button-wrapper {
  .one {
    background: white;
  }
  .two {
    background: black;
  }
  &:not(.one-is-active) {
    .one {
      background: black;
    }
    .two {
      background: white;
    }
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If I understand correctly, you want to have functionality similar to a radio button group, where if you click on one, you want the color of the other one to revert.

In order to do that, the buttons need to share state.

Where/how this state is shared can be implemented many different ways, but for this example I'll suggest it is stored in the parent component. The state can also be implemented using composition API, or options API, I will assume based on the level of experience that options may be a better fit.

<script>
export default {
  data() {
    return {
      selected: 0
    }
  },
}
</script>

<template>
  <button :class="{red:selected == 1}" @click="selected = 1">
    Button 1
  </button>
  
  <button :class="{red:selected == 2}" @click="selected = 2">
    Button 2
  </button>
</template>

<style>
  .red{
    background: red;
  }
</style>

example in vue SFC playground

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