Skip to content

Commit 46e1536

Browse files
committed
✨ add previous and next button event
1 parent f815077 commit 46e1536

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

music-player/src/components/SongList.vue

+16
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<SongPlayer
2929
:song="list[currentSongIndex]"
3030
@goback="isPlayerVisible = !isPlayerVisible"
31+
@next="playNext"
32+
@previous="playPrevious"
3133
/>
3234
</div>
3335
</template>
@@ -79,6 +81,20 @@ export default {
7981
this.currentSongIndex = index;
8082
this.isPlayerVisible = true;
8183
},
84+
playNext() {
85+
if (this.currentSongIndex < this.list.length - 1) {
86+
this.currentSongIndex += 1;
87+
} else {
88+
this.currentSongIndex = 0;
89+
}
90+
},
91+
playPrevious() {
92+
if (this.currentSongIndex != 0) {
93+
this.currentSongIndex -= 1;
94+
} else {
95+
this.currentSongIndex = this.list.length - 1;
96+
}
97+
},
8298
},
8399
};
84100
</script>

music-player/src/components/SongPlayer.vue

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<p class="text-gray-400">{{ song.year }}</p>
1717
</div>
1818
<div class="grid grid-cols-3 mt-10">
19-
<div class="flex items-center justify-center">
19+
<div class="flex items-center justify-center" @click="previous">
2020
<button>Previous</button>
2121
</div>
2222
<div class="flex items-center justify-center">
@@ -26,7 +26,7 @@
2626
Play
2727
</button>
2828
</div>
29-
<div class="flex items-center justify-center">
29+
<div class="flex items-center justify-center" @click="next">
3030
<button>Next</button>
3131
</div>
3232
</div>
@@ -47,11 +47,17 @@ export default {
4747
songSrc: String,
4848
},
4949
},
50-
emits: ["goback"],
50+
emits: ["goback", "next", "previous"],
5151
methods: {
5252
goback() {
5353
this.$emit("goback");
5454
},
55+
next() {
56+
this.$emit("next");
57+
},
58+
previous() {
59+
this.$emit("previous");
60+
},
5561
},
5662
};
5763
</script>

0 commit comments

Comments
 (0)