<template>
<div class="music-list">
<div class="back" @click="back">
<i class="icon-back"></i>
</div>
<h1 class="title" v-html="title"></h1>
<div class="bg-image" :style="bgStyle" ref="bgImage">
<div class="play-wrapper">
<div class="play" v-show="songs.length>0" ref="playBtn" @click="playRandom">
<i class="icon-play"></i>
<span class="text">随機全部播放</span>
</div>
</div>
<div class="filter" ref="filter"></div>
</div>
<div class="bg-layer" ref="layer"></div>
<scroll :data="songs" @scroll="scroll" :probe-type="probeType" :listern-scroll="listernScroll" class="list" ref="list">
<div class="song-list-wrapper">
<song-list :songs="songs" :rank="rank" @select="selectItem"></song-list>
</div>
<div class="loading-container" v-show="!songs.length">
<loading></loading>
</div>
</scroll>
</div>
</template>
<script>
import Scroll from '../../base/scroll/scroll'
import SongList from '../../base/song-list/song-list'
import {preFixStyle} from '../../common/js/mdom'
import Loading from '../../base/loading/loading'
import {mapActions} from 'vuex'
import {playlistMixin} from 'common/js/mixin'
const RESERVED_HEIGHT = // 頭部流出的距離
const transform = preFixStyle('transform')
export default {
mixins: [playlistMixin],
props: {
bgImage: {
type: String, // 實際上是圖像的連結位址 是以要通過計算屬性 獲得完整圖檔
default: ''
},
songs: {
type: Array,
default: () => {
return []
}
},
title: {
type: String,
default: ''
},
rank: {
type: Boolean,
default: false
}
},
data () {
return {
scrollY:
}
},
mounted () {
// 動态計算listtop值
// console.log(this.$refs.list) style在$el中
this.imgHeight = this.$refs.bgImage.clientHeight
this.minHeight = -this.$refs.bgImage.clientHeight + RESERVED_HEIGHT
this.$refs.list.$el.style.top = `${this.imgHeight}px` // 因為list是個元件,是以他的style在$el中
},
created () {
this.probeType =
this.listernScroll = true
},
methods: {
handlePlayList (playList) {
const bottom = playList.length > ? '60px' : ''
this.$refs.list.$el.style.bottom = bottom
this.$refs.list.refresh()
},
scroll (position) {
this.scrollY = position.y // 滾動的y坐标
},
back () {
this.$router.back() // 通過路由傳回上級路由
},
selectItem (item, index) {
this.selectPlay({
list: this.songs,
index
})
},
playRandom () {
this.randomPlay({
list: this.songs
})
},
...mapActions([
'selectPlay', // 調用action中的方法
'randomPlay'
])
},
watch: {
scrollY (newY) {
let translateY = Math.max(this.minHeight, newY)
let zIndex =
let scale =
let blur = // 模糊度
// 添加一個遮罩層,用來調節背景圖的高度。它發生變化,它下面的list也跟着變化
this.$refs.layer.style[transform] = `translate3d(, ${translateY}px, )`
// this.$refs.layer.style['webkitTransform'] = `translate3d(0, ${translateY}px, 0)`
const percent = Math.abs(newY / this.imgHeight) // 計算出下滑與圖檔背景的比例
if (newY > ) { // 往下滑
scale = + percent // 縮放
zIndex =
} else {
blur = Math.min( * percent, ) // 最小模糊度為20
}
this.$refs.filter.style['backdrop-filter'] = `blur(${blur}px)`
this.$refs.filter.style['filter'] = `blur(${blur}px)`
// this.$refs.filter.style['filter'] = `contrast(${blur}%)`
this.$refs.filter.style['webkitFilter'] = `blur(${blur}px)`
if (newY < this.minHeight) { // 往上滑
zIndex =
this.$refs.bgImage.style.paddingTop = // 通過padding-top設定高度
this.$refs.bgImage.style.height = `${RESERVED_HEIGHT}px`
this.$refs.playBtn.style.display = 'none'
} else {
this.$refs.bgImage.style.paddingTop = '70%' // 通過padding-top設定高度
this.$refs.bgImage.style.height =
this.$refs.playBtn.style.display = 'block'
}
this.$refs.bgImage.style.zIndex = zIndex // 改變背景圖的zindex
this.$refs.bgImage.style[transform] = `scale(${scale})` // scale縮放比例 詳情見transform:scale()
// this.$refs.bgImage.style['webkitTransform'] = `scale(${scale})`
}
},
computed: {
bgStyle () {
return `background-image: url(${this.bgImage})` // 通過屬性給dom動态添加背景圖
}
},
components: {
Scroll,
SongList,
Loading
}
}
</script>
<style lang="stylus">
@import "~common/stylus/variable"
@import "~common/stylus/mixin"
.music-list
position: fixed
z-index:
top:
left:
bottom:
right:
background: $color-background
.back
position absolute
top:
left: px
z-index:
.icon-back
display: block
padding: px
font-size: $font-size-large-x
color: $color-theme
.title
position: absolute
top:
left: %
z-index:
width: %
no-wrap()
text-align: center
line-height: px
font-size: $font-size-large
color: $color-text
.bg-image
position: relative
width: %
height:
padding-top: %
transform-origin: top // 基準點 transform-origin
background-size: %
.play-wrapper
position: absolute
bottom: px
z-index:
width: %
.play
box-sizing: border-box
width: px
padding: px
margin: auto
text-align: center
border: px solid $color-theme
color: $color-theme
border-radius: px
font-size:
.icon-play
display: inline-block
vertical-align: middle
margin-right: px
font-size: $font-size-medium-x
.text
display: inline-block
vertical-align: middle
font-size: $font-size-small
.filter
position: absolute
top:
left:
width: %
height: %
background: rgba(, , , )
background-size: cover
.bg-layer
position: relative
height: %
background: $color-background
.list
position: fixed
top:
bottom:
width: %
// z-index -
background: $color-background
.song-list-wrapper
padding: px px
.loading-container
position: absolute
width: %
top: %
transform: translateY(-%)
</style>
主要是滑動歌曲list時的互動邏輯需要多多注意
vuex中actions:
// 點選播放:分兩種情況: 順序/随機
export const selectPlay = function ({commit, state}, {list, index}) {
commit(types.SET_SEQUENCELIST, list) // 映射到state
if (state.mode === playMode.random) {
let randomList = shuffle(list) // 洗牌函數,徹底打亂,傳回一個新數組
commit(types.SET_PLAY_LIST, randomList)
index = findIndex(randomList, list[index]) // 從數組中尋找該item,并傳回其index
} else {
commit(types.SET_PLAY_LIST, list) // 順序播放
}
commit(types.CURRENTINDEX, index)
commit(types.SET_PLAY_SCREEN, true)
commit(types.SET_PLAYING_STATE, true)
}
//随機播放模式
export const randomPlay = function ({commit}, {list}) {
commit(types.MODE, playMode.random)
commit(types.SET_SEQUENCELIST, list)
let randomList = shuffle(list)
commit(types.CURRENTINDEX, )
commit(types.SET_PLAY_LIST, randomList)
commit(types.SET_PLAY_SCREEN, true)
commit(types.SET_PLAYING_STATE, true)
}
shuffle洗牌函數
function getRandom (min, max) { // 獲得min max之間的随機整數
return Math.floor(Math.random() * (max - min + ) + min) // (max - min + 1) 為了保證能取到max
}
export function shuffle (arr) {
let _arr = arr.slice() // 傳回一個新數組 以便于不改變原數組
for (let i = ; i < _arr.length; i++) {
let j = getRandom(, i)
let t = _arr[i]
_arr[i] = _arr[j]
_arr[j] = t
}
return _arr
}
function findIndex (list, song) {
return list.findIndex((item) => { // es6 api 傳回index序号 如果傳回-1表示沒有
return item.id === song.id
})
}