至關明顯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); },
美團項目數組