I made this pretty simple content loop-carousel component, but the code seems too "Iffy" but Im not sure how I could make it better.
I tried to use the Switch expression, which makes it a bit more readable, but I dont like the breaks in them.
here is the relevant js code:
data() {
return {
...
currentContentId: 0,
content: [
{ id: 0, title: "title1", body: "description1" },
{ id: 1, title: "title2", body: "description2" },
{ id: 2, title: "title3", body: "description3" }
]
};
},
methods: {
nav(e) {
let loopLength = this.content.length - 1;
if (e.target.parentElement.id === "nav__back") {
if (this.currentContentId == 0) {
this.currentContentId = loopLength
} else {
this.currentContentId--
}
} else {
if (this.currentContentId == loopLength) {
this.currentContentId = this.currentContentId - loopLength
} else {
this.currentContentId++
}
}
}
html:
<div class="content">
<a id="nav__back" class="content__arrow" @click="nav">
<i class="material-icons">arrow_back_ios</i>
</a>
<div class="content__wrapper">
<h1 class="content__heading">{{ content[currentContentId].title }}</h1>
<div class="content__text">
<p>{{ content[currentContentId].body }}</p>
</div>
</div>
<a id="nav__forward" class="content__arrow" @click="nav">
<i class="material-icons">arrow_forward_ios</i>
</a>
</div>
heres my codepen link (theres also a theme switcher, pay no mind to that)