Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
Whenever i click next or prev on my image slider it scrolls to the top of page how do i stop this?
<script>
export default {
  name: "Slider",
  data() {
    return {
      images: [
        "https://cdn.pixabay.com/photo/2015/12/12/15/24/amsterdam-1089646_1280.jpg",
        "https://cdn.pixabay.com/photo/2016/02/17/23/03/usa-1206240_1280.jpg",
        "../assets/sample-1.jpg"
        "https://cdn.pixabay.com/photo/2016/12/04/19/30/berlin-cathedral-1882397_1280.jpg"
      ],
      
      currentIndex: 0
    };
  },


  methods: {
 

    next: function() {
      this.currentIndex += 1;
    },
    prev: function() {
      this.currentIndex -= 1;
    }
  },

  computed: {
    currentImg: function() {
      return this.images[Math.abs(this.currentIndex) % this.images.length];
    }
  }
};
</script>

vue.js below

<template>
 <div>
    <transition-group name="fade" tag="div">
      <div v-for="i in [currentIndex]" :key="i">
        <img :src="currentImg" />
      </div>
    </transition-group>
    <a class="prev" @click="prev" href="#">&#10094; Previous</a>
    <a class="next" @click="next" href="#">&#10095; Next</a>
    </div>

Just scrolls to the top every time i click on either prev or next can't figure out why. Also i havent been able to get any of my own images from assets to appear in the slider and not sure why it isnt able to retrieve them this way (as in the sample-1.jpg) Thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. Remove href="#"
  2. You dont need v-for or transition-group. A simple transition with mode="out-in" is enough. You put the key to the current image, so vue always knows if the image gets replaced so it can do a transition

Dont put images into the assets folder. You better put it into the static folder.

Then you can access your image like /sample-1.jpg

new Vue({
  name: "Slider",
  el: "#app",
  data() {
    return {
      images: [
        "https://cdn.pixabay.com/photo/2015/12/12/15/24/amsterdam-1089646_1280.jpg",
        "https://cdn.pixabay.com/photo/2016/02/17/23/03/usa-1206240_1280.jpg",
        "/sample-1.jpg",
        "https://cdn.pixabay.com/photo/2016/12/04/19/30/berlin-cathedral-1882397_1280.jpg"
      ],
      currentIndex: 0
    };
  },
})
.v-enter-active,
.v-leave-active {
  transition: opacity 1s ease;
}

.v-enter,
.v-leave-to {
  opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <a class="prev" @click="currentIndex--" v-if="currentIndex > 0">&#10094; Previous</a>
    <a class="next" @click="currentIndex++" v-if="currentIndex < images.length - 1">&#10095; Next</a>
    <transition name="v" mode="out-in">
        <img :key="images[currentIndex]" :src="images[currentIndex]" />
    </transition>
  
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!