1

How can I pass a variable from Images Array to access the display different image everytime I press the next button? I am new to vue js, I could use some help.

<template>
  <div class="background" :style="{backgroundImage: `url(${Images[number]})`}">
    <div v-on:click="next" class="nextButton"><p>&gt;</p></div>   
    </div>
  </div>
</template>

this is the array

data: () => ({
    Images:[ Image1, Image2, Image3],

this is my next script code

  methods:{
    next()
    { 
      var number = 0;

      if(number > 2){
        number == 0;
      }
      else{
        number ++;
      }
    },
  }

1 Answer 1

2

The problem is everytime when you call the next method, you initialize the number to be zero. Please check the correct solution below

data: () => ({
    Images:[ Image1, Image2, Image3],
    number: 0, // Change added
    })

and the method would be like

methods:{
    next()
    { 
      if(this.number >= this.Images.length){ // Change added
        this.number = 0; // Change added
      }
      else{
        this.number += 1; // Change added
      }
    },
  }
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much sir now I understand how it works.
Great!! I'm glad that helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.