1313 </h1 >
1414 </div >
1515
16- <BaseScroll class =" list-content" :data =" sequenceList" >
16+ <BaseScroll ref = " contentScroll " class =" list-content" :data =" sequenceList" >
1717 <ul >
1818 <li
19+ ref =" playlistItem"
1920 class =" content-item"
2021 v-for =" (song, index) of sequenceList"
2122 :key =" index"
22- @click =" selectItem (song, index)"
23+ @click =" selectSong (song, index)"
2324 >
24- <i class =" current" ></i >
25+ <i : class =" [' current', getCurrentIcon(song)] " ></i >
2526 <span class =" item-text" >{{ song.name }}</span >
2627 <span class =" like-btn" >
2728 <i class =" icon-not-like" ></i >
@@ -71,7 +72,24 @@ export default {
7172 },
7273
7374 methods: {
74- selectItem (song , index ) {
75+ scrollToCurrentSong (current ) {
76+ const index = this .sequenceList .findIndex (item => {
77+ return current .id === item .id
78+ })
79+
80+ this .$refs .contentScroll .scrollToElement (this .$refs .playlistItem [index], 300 )
81+ },
82+
83+ getCurrentIcon (item ) {
84+ if (this .currentSong .id === item .id ) {
85+ return ' icon-play'
86+ } else {
87+ return ' '
88+ }
89+ },
90+
91+ selectSong (song , index ) {
92+ // 传入的 Index 即是展示性列表 sequenceList 中的 Index
7593 if (this .mode === playMode .random ) {
7694 index = this .playlist .findIndex (item => {
7795 return song .id === item .id
@@ -114,6 +132,11 @@ export default {
114132
115133 showPlaylist () {
116134 this .hasShowPlaylist = true
135+ // 只有在 display 非 none 值之后才是正确的计算元素高度的时机
136+ setTimeout (() => { // this.$nextTick
137+ this .$refs .contentScroll .refresh ()
138+ this .scrollToCurrentSong (this .currentSong )
139+ }, 20 )
117140 },
118141
119142 closePlaylist () {
@@ -128,6 +151,16 @@ export default {
128151 })
129152 },
130153
154+ watch: {
155+ currentSong (newSong , oldSong ) {
156+ if (! this .showPlaylist || newSong .id === oldSong .id ) {
157+ return
158+ }
159+
160+ this .scrollToCurrentSong (newSong)
161+ }
162+ },
163+
131164 computed: {
132165 playModeIcon () {
133166 return this .mode === 0 ? ' icon-sequence' : this .mode === 1 ? ' icon-loop' : ' icon-random'
0 commit comments