vue-music(20)点击播放及当前博凡歌曲在列表中的显示的实现

给当前播放的歌曲添加一个class

<i class="current" :class="getActivatyCls(item)"></i>

getActivatyCls (item) {
      if (this.currentSong.id === item.id) { // 当前播放是选中的歌曲
        return 'icon-play' // 显示这个class
      }
    },

在列表中添加点击事件:

<li class="item" v-for="(item, index) of sequenceList" :key="item.id" @click="selectItem(item, index)">

selectItem (item, index) {
      if (this.mode === playMode.random) {
        // 如果当前模式是随机播放
        index = this.playList.findIndex((song) => {
          return song.id === item.id
          // 就找到当前点击的歌曲所对应的item索引
        })
      }
      this.setCurrentIndex(index) // actions方法
      this.playing(true)
    },

scrollToCurrent的实现:

scrollToCurrent (current) {
      const index = this.sequenceList.findIndex((song) => {
        return current.id === song.id
        // 找到current在song里面的位置
      })
      this.$refs.listContent.scrollToElement(this.$refs.listItem[index], 300)
    },

然后监控currentSong的变换,如果发生变化,就调用scrollToCurrent的方法:

watch: {
    currentSong (newSong, oldSong) {
      if (!this.showFlag || newSong === oldSong) {
        return
      }
      this.scrollToCurrent(newSong)
    }
  },

同时在show的时候,也要调用这个方法:

show () {
      this.showFlag = true
      setTimeout(() => {
        this.$refs.listContent.refresh()
        this.scrollToCurrent(this.currentSong)
      })
    },
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值