小程式 字母選擇排序
wxml {{group.groupName}} {{user.name}}
this.setData({ eachLetterHeight: eachLetterHeight }); // 求各字母距離裝置左上角所處位置 for(let i = 0, len = letters.length; i < len; i++) { const x = this.data.windowWidth - (10 + 50) / this.data.pixelRatio, y = comTop + (i * eachLetterHeight); temp.push([x, y]); } this.setData({ lettersPosition: temp })
}, tabLetter(e) { const index = e.currentTarget.dataset.index; this.setData({ selected: index, scrollIntoView: index })
this.cleanAcitvedStatus();
}, // 清除字母選中狀態 cleanAcitvedStatus() { setTimeout(() => { this.setData({ selected: 0 }) }, 500); }, touchmove(e) { const x = e.touches[0].clientX, y = e.touches[0].clientY, lettersPosition = this.data.lettersPosition, eachLetterHeight = this.data.eachLetterHeight, letters = this.data.letters; console.log(y); // 判斷觸控點是否在字母導航欄上 if(x >= lettersPosition[0][0]) { for(let i = 0, len = lettersPosition.length; i < len; i++) { // 判斷落在哪個字母區域,取出對應字母所在陣列的索引,根據索引更新selected及scroll-into-view的值 const _y = lettersPosition[i][1], // 單個字母所處高度 __y = _y + eachLetterHeight; // 單個字母最大高度取值範圍, 50為字母高50rpx if(y >= _y && y <= __y) { this.setData({ selected: letters[i], scrollIntoView: letters[i] }); break; } } } }, touchend(e) { this.cleanAcitvedStatus(); } }) css page { background-color: #eee; } .nav { position: fixed; right: 10rpx; top: 3%; height: 94%; width: 50rpx; font-family: Arial, Helvetica, sans-serif } .letter { width: 50rpx; height: 50rpx; font-size: 30rpx; } .letter-text { display: inline-block; width: 100%; height: 100%; text-align: center; line-height: 50rpx; border-radius: 50%; } .letter-actived { background-color: #ccc; } .groups { /height: 100%;