前言
其實很簡單只是用了js的幾個函數
substr
unshift
splice
完整代碼 | mpvue模仿QQ
代碼
思路很簡單,獲取當前元素下標然后通過unshift
函數將該值插入到數組第一位,再通過splice
函數刪除該值
核心代碼
listTop (e) {
const that = this
let index = e.currentTarget.dataset.eventid.substr(e.currentTarget.dataset.eventid.length - 1, 1)
that.messageList.unshift(that.messageList[index])
that.messageList.splice(++index, 1)
}