至关明显vue
把左侧菜单的一项成为分类(category),
把右侧每一项称为项(item)。git
jumpArr
;y
值在jumpArr
中的位置,更新左侧category样式。每次触发scroll事件后都会遍历数组jumpArr
来判断此刻左侧导航该有的样式。scroll事件的触发是至关密集的,也就是jumpArr
的遍历也是至关密集。对性能有很大的影响。特别是当应用愈来愈大时、设备比较老旧时。github
jumpArr
;if...else
也能够使用switch...case
,这里选用后者,由于性能更加出众。jumpFunc
是字符串,须要使用eval()
来进行函数转换。jumpArr
getGoodsHeight() { //这两项是用来获取content总高的 const specials = document.querySelector(".specials"); const itemList = document.querySelectorAll(".group-item"); //预赛了 let heightList = []; heightList.push(specials.offsetHeight); itemList.forEach((element, index) => { const eleHeight = element.offsetHeight; const preheight = heightList[index]; heightList.push(preheight + eleHeight); }); this.heightList = heightList; }
jumpArr
得出用于逻辑处理的跳转函数jumpFunc
getJumpFunc() { //这样的写法有助于性能,使用判断而不是用循环 let i = 0; let func = `(function () { return function (y, context) {`; const length = this.heightList.length; while (i < length) { const current = this.heightList[i - 1] || 0; const next = this.heightList[i]; const lastIndex = length - 1; let partition = ""; switch (i) { case 0: partition = `if(y > 0 && y< ${next}) { context.addNavStyle(${i}) }`; break; case lastIndex: partition = `else { context.addNavStyle(${i}) }};})();`; break; default: partition = `else if (y > ${current} && y <${next}) { context.addNavStyle(${i}) }`; break; } func += partition; i++; } return eval(func); },
美团项目数组