knuth shuffle
export function shuffle(source) {
const arr = source.slice() //避免产生副作用,修改原始的值
for (let i = 0; i < arr.length; i++) {
const j = getRandomInt(i)
swap(arr, i, j)
}
return arr
}
function getRandomInt(max) {
return Math.floor(Math.random() * (max + 1))
}
function swap(arr, i, j) {
const t = arr[i]
arr[i] = arr[j]
arr[j] = t
}
store
export function randomPlay({ commit }, list) {
commit('setPlayMode', PLAY_MODE.random)
commit('setSequenceList', list)
commit('setPlayingState', true)
commit('setFullScreen', true)
commit('setPlaylist', shuffle(list))
commit('setCurrentIndex', 0)
}
动态改变样式
页面向上滚动到一定位置,随机播放按钮不再显示。
<div
class="play-btn-wrapper"
:style="playBtnStyle"
>
<div
v-show="songs.length > 0"
class="play-btn"
@click="random"
>
<i class="icon-play"></i>
<span class="text">随机播放全部</span>
</div>
</div>
computed: {
playBtnStyle() {
let display = ''
if (this.scrollY >= this.maxTranslateY) {
display = 'none'
}
return {
display
}
},
}
标签:function,arr,shuffle,random,歌曲,随机,commit,播放,const
From: https://www.cnblogs.com/poco-o/p/16862231.html