小程序組件之仿微信通訊錄的實(shí)現(xiàn)代碼
最近模仿微信通信錄做了個(gè)小程序組件,分享給大家,具體如下:
效果圖

因?yàn)槭鞘褂玫氖謾C(jī)錄屏,視頻格式為MP4,上傳到文章時(shí)發(fā)現(xiàn)只支持圖片,還好電腦自動(dòng)錄屏功能,所以簡(jiǎn)單的錄制了一下,完后又提示只能4M,只能再去壓縮圖片,所以畫(huà)質(zhì)略渣,各位客官講究的看看吧。
特色功能介紹
- 用戶(hù)只需按照格式傳入?yún)?shù),組件能夠自動(dòng)將參數(shù)按首字母分組,簡(jiǎn)單方便;
- 組件右側(cè)首字母導(dǎo)航無(wú)需另外傳值,并且根據(jù)參數(shù)具體有哪些首字母顯示(沒(méi)有的咱就不要);
- 用戶(hù)進(jìn)行上下滑動(dòng)時(shí),左右相互聯(lián)動(dòng);
- 點(diǎn)擊右側(cè)導(dǎo)航,組件會(huì)相應(yīng)的上下滾動(dòng)。
實(shí)現(xiàn)基礎(chǔ)
本組件只使用了小程序基礎(chǔ)組件中的scroll-view,不用那么麻煩,簡(jiǎn)單方便,一看就懂,哈哈哈
wxml
滾動(dòng)區(qū)域
<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è)面。 但是請(qǐng)注意:很多同學(xué)會(huì)發(fā)現(xiàn)設(shè)置了高度100%后,組件并沒(méi)有效果,這是因?yàn)槟銢](méi)有將頁(yè)面高度設(shè)置為100%,所以你還需在app.wxss中設(shè)置page的高度為100%; 其他的屬性看文檔就好,我就不再多說(shuō);
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)航
<view class="list-fixed {{fixedTitle=='' ? 'hide':''}}" style="transform:translate3d(0,{{fixedTop}}px,0);">
<view class="fixed-title">
{{fixedTitle}}
</view>
</view>
js
渲染參數(shù)
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)
},
計(jì)算分組高度
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][3]
使用該方法獲取到各分組的高度,存入lHeight中用于之后滾動(dòng)時(shí)判斷使用;
同學(xué)們可以看到我在將lHeight賦值給data的listHeight時(shí)使用了定時(shí)器,這是因?yàn)楂@取節(jié)點(diǎn)信息api是異步執(zhí)行的,顧你直接進(jìn)行賦值是沒(méi)有效果的,所以我使用了定時(shí)器功能;
**我覺(jué)得這里使用定時(shí)器不是最好的處理方式,同學(xué)們有更好的方法請(qǐng)告訴我,謝謝**
對(duì)滾動(dòng)事件進(jìn)行處理
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 (艾倫·沃克)",
},
]
最后
完整代碼請(qǐng)戳 gitHub
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ES6 javascript中Class類(lèi)繼承用法實(shí)例詳解
這篇文章主要介紹了ES6 javascript中Class類(lèi)繼承用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了ES6繼承的基本用法、相關(guān)屬性、方法與使用技巧,需要的朋友可以參考下2017-10-10
javascript結(jié)合CSS實(shí)現(xiàn)蘋(píng)果開(kāi)關(guān)按鈕特效
這篇文章主要介紹了javascript結(jié)合CSS實(shí)現(xiàn)蘋(píng)果開(kāi)關(guān)按鈕特效的方法以及全部代碼,效果非常不錯(cuò),兼容性也很好,有需要的小伙伴自己參考下2015-04-04
深入理解移動(dòng)前端開(kāi)發(fā)之viewport
這篇文章主要介紹了移動(dòng)前端開(kāi)發(fā)之viewport的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
javascript實(shí)現(xiàn)日期三級(jí)聯(lián)動(dòng)下拉框選擇菜單
這篇文章主要介紹了javascript實(shí)現(xiàn)日期三級(jí)聯(lián)動(dòng)下拉框選擇菜單,實(shí)現(xiàn)JS年月日三級(jí)聯(lián)動(dòng)下拉框選擇功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
ionic js 模型 $ionicModal 可以遮住用戶(hù)主界面的內(nèi)容框
這篇文章主要介紹了ionic js 模型 $ionicModal 可以遮住用戶(hù)主界面的內(nèi)容框的相關(guān)資料,需要的朋友可以參考下2016-06-06
使用JavaScript實(shí)現(xiàn)改造layer彈層移動(dòng)版組件
這篇文章主要為大家詳細(xì)介紹了使用JavaScript實(shí)現(xiàn)改造layer彈層移動(dòng)版組件的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
JS中map與forEach無(wú)法跳出循環(huán)及every和some的使用
在我們平時(shí)使用習(xí)慣中,for循環(huán)里要跳出整個(gè)循環(huán)是使用break,但在數(shù)組中用forEach循環(huán)或者map如要退出整個(gè)循環(huán)使用break會(huì)報(bào)錯(cuò),使用return也不能跳出循環(huán),下面這篇文章主要介紹了關(guān)于JS中map與forEach無(wú)法跳出循環(huán)及every和some的使用的相關(guān)資料,需要的朋友可以參考下2023-05-05

