效果圖話不多說(shuō),先上效果圖
因?yàn)槭鞘褂玫氖謾C(jī)錄屏,視頻格式為MP4,上傳到文章時(shí)發(fā)現(xiàn)只支持圖片,還好電腦自動(dòng)錄屏功能,所以簡(jiǎn)單的錄制了一下,完后又提示只能4M,只能再去壓縮圖片,所以畫(huà)質(zhì)略渣,各位客官講究的看看吧。 特色功能介紹
實(shí)現(xiàn)基礎(chǔ)
說(shuō)到滾動(dòng)當(dāng)然少不了小程序的基礎(chǔ)組件scroll-view,該組件就是在此基礎(chǔ)上實(shí)現(xiàn)的; Wxml
<scroll-view scroll-y style="height:100%;white-space:nowrap;" scroll-into-view="{{toView}}" enable-back-to-top bindscroll="scroll" scroll-with-animation scroll-top="{{scrollTop}}">
<view class="list-group" wx:for="{{logs}}" wx:for-item="group">
<view class="title" id="{{group.title}}">{{group.title}}</view>
<block wx:for="{{group.items}}" wx:for-item="user">
<view id="" class="list-group-item">
<image class="icon" src="{{user.avatar}}" lazy-load="true"></image>
<text class="log-item">{{user.name}}</text>
</view>
</block>
</view>
</scroll-view>
簡(jiǎn)單說(shuō)一下上述代碼:根據(jù)小程序文檔,在使用scroll-view組件用于豎向滾動(dòng)時(shí)一定要設(shè)置高度,你們可以看到我在代碼中設(shè)置了'height:100%;'這就實(shí)現(xiàn)了組件的滾動(dòng)高度是整個(gè)頁(yè)面。 2.側(cè)邊字母導(dǎo)航
<view class="list-shortcut">
<block wx:for="{{logs}}">
<text class="{{currentIndex===index?'current':''}}" data-id="{{item.title}}" bindtap='scrollToview'>{{item.title}}</text>
</block>
</view>
3.固定在頂部的字母導(dǎo)航 仔細(xì)的同學(xué)能發(fā)現(xiàn)在滾動(dòng)時(shí),頂部有一個(gè)固定位置的字母導(dǎo)航,其值對(duì)應(yīng)滾動(dòng)到的區(qū)域
<view class="list-fixed {{fixedTitle=='' ? 'hide':''}}" style="transform:translate3d(0,{{fixedTop}}px,0);">
<view class="fixed-title">
{{fixedTitle}}
</view>
</view>
Wxss樣式太簡(jiǎn)單了,就不發(fā)了,重點(diǎn)看js部分
js
normalizeSinger(list) {
//歌手列表渲染
let map = {
hot: {
title: this.data.HOT_NAME,
items: []
}
}
list.forEach((item, index) => {
if (index < this.data.HOT_SINGER_LEN) {
map.hot.items.push({
name: item.Fsinger_name,
avatar:this.constructor(item.Fsinger_mid)
})
}
const key = item.Findex
if (!map[key]) {
map[key] = {
title: key,
items: []
}
}
map[key].items.push({
name: item.Fsinger_name,
avatar: this.constructor(item.Fsinger_mid)
})
})
let ret = []
let hot = []
for (let key in map) {
let val = map[key]
if (val.title.match(/[a-zA-Z]/)) {
ret.push(val)
} else if (val.title === this.data.HOT_NAME) {
hot.push(val)
}
}
ret.sort((a, b) => {
return a.title.charCodeAt(0) - b.title.charCodeAt(0)
})
return hot.concat(ret)
},
將用戶(hù)數(shù)據(jù)分為兩大塊,即熱門(mén)組和不熱門(mén)組默認(rèn)將參數(shù)的前10組歸類(lèi)為熱門(mén)組,然后對(duì)所以參數(shù)安裝首字母進(jìn)行排序分組。
var lHeight = [],
that = this;
let height = 0;
lHeight.push(height);
var query = wx.createSelectorQuery();
query.selectAll('.list-group').boundingClientRect(function(rects){
var rect = rects,
len = rect.length;
for (let i = 0; i < len; i++) {
height += rect[i].height;
lHeight.push(height)
}
}).exec();
var calHeight = setInterval(function(){
if (lHeight != [0]) {
that.setData({
listHeight: lHeight
});
clearInterval(calHeight);
}
},1000)
在獲取元素屬性上,小程序提供了一個(gè)很方便的api,wx.createSelectotQuery();具體使用方法請(qǐng)看節(jié)點(diǎn)信息API
const listHeight = this.data.listHeight
// 當(dāng)滾動(dòng)到頂部,scrollY<0
if (scrollY == 0 || scrollY < 0) {
this.setData({
currentIndex:0,
fixedTitle:''
})
return
}
// 在中間部分滾動(dòng)
for (let i = 0; i < listHeight.length - 1; i++) {
let height1 = listHeight[i]
let height2 = listHeight[i + 1]
if (scrollY >= height1 && scrollY < height2) {
this.setData({
currentIndex:i,
fixedTitle:this.data.logs[i].title
})
this.fixedTt(height2 - newY);
return
}
}
// 當(dāng)滾動(dòng)到底部,且-scrollY大于最后一個(gè)元素的上限
this.setData({
currentIndex: listHeight.length - 2,
fixedTitle: this.data.logs[listHeight.length - 2].title
})
參數(shù)格式
list:[
{
"index": "X",
"name": "薛之謙",
},
{
"index": "Z",
"name": "周杰倫",
},
{
"index": "B",
"name": "BIGBANG (??)",
},
{
"index": "B",
"name": "陳奕迅",
},
{
"index": "L",
"name": "林俊杰",
},
{
"index": "A",
"name": "Alan Walker (艾倫·沃克)",
},
]
如果你們還需要其他的參數(shù),對(duì)應(yīng)的在后面加上即可。 END |