Vue
HTML
<div id="app">
<div class="wrapper" :style="wrapper"></div>
<button @click="previous">Previous</button>
<button @click="next">Next</button>
</div>
CSS
.wrapper {
width: 200px;
height: 200px;
}
Vue
new Vue({
el: "#app",
data: {
imgs: [
{ path: "https://picsum.photos/200/300" },
{ path: "https://picsum.photos/150/300" },
{ path: "https://picsum.photos/220/300" },
{ path: "https://picsum.photos/310/300" },
],
currentImgIndex: 0
},
computed: {
wrapper () {
return {
'background': `url(${this.imgs[this.currentImgIndex].path})`
}
}
},
methods: {
next () {
this.currentImgIndex >= this.imgs.length-1 ? this.currentImgIndex = 0 : this.currentImgIndex++
},
previous () {
this.currentImgIndex <= 0 ? this.currentImgIndex = this.imgs.length-1 : this.currentImgIndex--
}
}
})