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

87
Visualizações
How to treat each element as single over array in Vuejs

I have an array of products, I want to change the element src only on the one I'm hovering on. If there is a way of doing it in Vuejs I would like to know. My code so far

  <div class="swiper-slide" v-for="(prodotto,index) in prodotti" :key="prodotto.title">
   <img @mouseover="swapImage(index)" @mouseleave="swapImageLeave" class="swiper-lazy ima-prodotto" :key="index" :src=" hoverProdotti === 1 ? prodotto.images[0] : prodotto.images[1]" v- 
            bind:alt="prodotto.title">
       </div>

const app = new Vue({

    el: "#root",
  
    data: {

      hoverProdotti:0,
  
    },
  
    methods: {
  
      swapImage: function(index) {
       console.log(index)
        this.hoverProdotti = 1
      },
      swapImageLeave: function() {
    
        this.hoverProdotti = 0
      },





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

0

Assuming that your prodotto.images is an array with couple of images in it and your requirement is to show the image on the 0th index when hovered and show the image on the 1st index when un-hovered.

<div class="swiper-slide" v-for="(prodotto,index) in prodotti" :key="prodotto.title">
  <img 
    @mouseover="hoverProdotti = index" 
    @mouseleave="hoverProdotti = null" 
    class="swiper-lazy ima-prodotto" 
    :key="index" 
    :src="hoverProdotti === index ? prodotto.images[0] : prodotto.images[1]" 
    :alt="prodotto.title"
  />
</div>

data: {
  hoverProdotti:null,
},

Code explanation:

Assign 'null' value to hoverProdotti variable. Inside the v-for when you mouse over on a specific element, it's index gets assigned to hoverProdotti, which would fulfill the condition we have placed in :src. And as soon as mouse leaves, hoverProdotti gets null value assigned.

With this logic, it will swap images on your mouseover and mouseleave for the all the elements of the array.

Note: No need of methods or any functions.

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