小程序開發(fā)組件的使用:仿微信通訊錄

類似微信通訊錄的形式,要怎么開發(fā)小程序的頁面,下面為大家介紹類似通訊錄的小程序組件樣式
特色功能介紹- 用戶只需按照格式傳入?yún)?shù),組件能夠自動將參數(shù)按首字母分組,簡單方便;
- 組件右側(cè)首字母導(dǎo)航無需另外傳值,并且根據(jù)參數(shù)具體有哪些首字母顯示(沒有的咱就不要);
- 用戶進行上下滑動時,左右相互聯(lián)動;
- 點擊右側(cè)導(dǎo)航,組件會相應(yīng)的上下滾動。
說到滾動當(dāng)然少不了小程序的基礎(chǔ)組件scroll-view,該組件就是在此基礎(chǔ)上實現(xiàn)的;
監(jiān)聽組件scroll-view的bindscroll事件,進而對組件數(shù)據(jù)進行操作,即可完成。
- 滾動區(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>
簡單說一下上述代碼:根據(jù)小程序文檔,在使用scroll-view組件用于豎向滾動時一定要設(shè)置高度,你們可以看到我在代碼中設(shè)置了'height:100%;'這就實現(xiàn)了組件的滾動高度是整個頁面。
但是請注意:很多同學(xué)會發(fā)現(xiàn)設(shè)置了高度100%后,組件并沒有效果,這是因為你沒有將頁面高度設(shè)置為100%,所以你還需在app.wxss中設(shè)置page的高度為100%
其他的屬性看文檔就好,我就不再多說;
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)航
仔細的同學(xué)能發(fā)現(xiàn)在滾動時,頂部有一個固定位置的字母導(dǎo)航,其值對應(yīng)滾動到的區(qū)域
<view class="list-fixed {{fixedTitle=='' ? 'hide':''}}" style="transform:translate3d(0,{{fixedTop}}px,0);"> <view class="fixed-title"> {{fixedTitle}} </view> </view>Wxss
樣式太簡單了,就不發(fā)了,重點看js部分
js- 拿到參數(shù)第一步當(dāng)然是將參數(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) },
將用戶數(shù)據(jù)分為兩大塊,即熱門組和不熱門組默認(rèn)將參數(shù)的前10組歸類為熱門組,然后對所以參數(shù)安裝首字母進行排序分組。
- 計算各組高度
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)
在獲取元素屬性上,小程序提供了一個很方便的api,wx.createSelectotQuery();具體使用方法請看節(jié)點信息API
使用該方法獲取到各分組的高度,存入lHeight中用于之后滾動時判斷使用;
同學(xué)們可以看到我在將lHeight賦值給data的listHeight時使用了定時器,這是因為獲取節(jié)點信息api是異步執(zhí)行的,顧你直接進行賦值是沒有效果的,所以我使用了定時器功能;
我覺得這里使用定時器不是最好的處理方式,同學(xué)們有更好的方法請告訴我,謝謝
3.第三步就是在滾動的時候獲取滾動高度,相應(yīng)的處理即可,滾動使用到了scroll-view自帶事件,這個事件會返回滾動的距離,及其方便
const listHeight = this.data.listHeight // 當(dāng)滾動到頂部,scrollY<0 if (scrollY == 0 || scrollY < 0) { this.setData({ currentIndex:0, fixedTitle:'' }) return } // 在中間部分滾動 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)滾動到底部,且-scrollY大于最后一個元素的上限 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ù),對應(yīng)的在后面加上即可。
第二部分:如何開通一個小商店
1、本网站发布的该篇文章,目的在于分享电商知识及传递、交流相关电商信息,以便您学习或了解电商知识,请您不要用于其他用途;
2、该篇文章中所涉及的商标、标识的商品/服务并非来源于本网站,更非本网站提供,与本网站无关,系他人的商品或服务,本网站对于该类商标、标识不拥有任何权利;
3、本网站不对该篇文章中所涉及的商标、标识的商品/服务作任何明示或暗示的保证或担保;
4、本网站不对文章中所涉及的内容真实性、准确性、可靠性负责,仅系客观性描述,如您需要了解该类商品/服务详细的资讯,请您直接与该类商品/服务的提供者联系。