<view class="indexes_chunk" wx:for="{{brandIndexList.brandGroupList}}" wx:key="item">
<view class="letter ancehor-{{item.indexLetter}}">{{item.indexLetter}}</view>
<view class="choice" wx:for="{{item.brandList}}" wx:for-item="items" wx:key="items" wx:for-index="cindex">
<text class="text actives">{{items.enName}}{{items.cnName}}</text>
<text class="iconfont icon-gouxuan"></text>
</view>
</view>
function throttle(fn, interval) {
var enterTime = 0;
var gapTime = interval || 300 ;
return function() {
var context = this;
var backTime = new Date();
if (backTime - enterTime > gapTime) {
fn.call(context,arguments);
enterTime = backTime;
}
};
};
data:{
brandIndexList:{
brandGroupList:[
{
brandList:[
{brandId:1, cnName: "爱马仕A", enName: "Hermes", indexLetter: "A"}
],
indexLetter: "A"
},
{
brandList:[
{brandId:2, cnName: "爱马仕B", enName: "Hermesss", indexLetter: "B"}
],
indexLetter: "B"
},
{
brandList:[
{brandId:3, cnName: "爱马仕G", enName: "Hermes", indexLetter: "G"}
],
indexLetter: "G"
},
{
brandList:[
{brandId:4, cnName: "爱马仕M", enName: "Hermesss", indexLetter: "M"}
],
indexLetter: "M"
},
{
brandList:[
{brandId:5, cnName: "爱马仕P", enName: "Hermesss", indexLetter: "P"}
],
indexLetter: "P"
},
{
brandList:[
{brandId:6, cnName: "爱马仕V", enName: "Hermesss", indexLetter: "V"}
],
indexLetter: "V"
}
],
indexLetterList: ["A", "B", "G", "M", "P", "V"]
},
letterNodes:[],
letterIndex:0,
}
onReady(){
let self = this;
let indexLetterList = self.data.brandIndexList.indexLetterList,
letterNodes = self.data.letterNodes,
nodes = '.ancehor-',
arrs = [];
indexLetterList.forEach((item)=>{
arrs.push(nodes+item);
if(arrs.length == indexLetterList.length){
self.getDoms(arrs.join(','),(res)=>{
letterNodes = res.map((item)=>{return item.top-88});
self.setData({
letterNodes:letterNodes
});
});
wx.hideLoading();
}
});
},
getDoms(node,success){
let self = this,
query = wx.createSelectorQuery();
setTimeout(()=>{
query.selectAll(node).boundingClientRect((res)=>{
success && success(res);
}).exec()
},1000);
},
letterClick(e){
let self = this,
index = e.currentTarget.dataset.index,
letterNodes = self.data.letterNodes;
wx.pageScrollTo({
scrollTop: letterNodes[index]
})
},
onPageScroll:throttle(function(e){
let self = this,
scrollTop = e[0].scrollTop,
stickyTop = self.data.stickyTop,
letterNodes = self.data.letterNodes,
showSticky = self.data.showSticky,
letterIndex = self.data.letterIndex;
if(scrollTop>=stickyTop){
showSticky = true;
}else{
showSticky = false;
}
letterNodes.forEach((item,index)=>{
if(scrollTop>=item){
letterIndex = index;
}
})
self.setData({
showSticky:showSticky,
letterIndex:letterIndex
});
},10)
|