[{"data":1,"prerenderedAt":623},["ShallowReactive",2],{"\u002Farticles\u002Fupdating-a-menu-based-on-page-location":3},{"id":4,"title":5,"body":6,"createdAt":614,"description":615,"extension":616,"meta":617,"navigation":263,"path":618,"rawbody":619,"seo":620,"stem":621,"__hash__":622},"articles\u002Farticles\u002Fupdating-a-menu-based-on-page-location.md","Updating a Menu Based on Page Location",{"type":7,"value":8,"toc":612},"minimark",[9,19,25,28,233,240,538,541,559,562,578,605,608],[10,11,12,13,18],"p",{},"There are plenty of sites that have sectioned pages which use an anchored link menu system to jump to each section. If you look back at ",[14,15,17],"a",{"href":16},"\u002Farticles\u002Fsmooth-anchors","my smooth anchor post",", you can easily adapt that to make the jump behave a little bit nicer. But how about updating the menu so the user is reminded what section they’re on as they scroll down the page? It’s just a few lines of JavaScript+jQuery and you’ll be updating your menu on-the-fly. Let’s get started.",[10,20,21],{},[22,23,24],"em",{},"Note: Be sure to include a copy of jQuery or this won’t work.",[10,26,27],{},"First, let’s setup our menu’s HTML:",[29,30,35],"pre",{"className":31,"code":32,"language":33,"meta":34,"style":34},"language-html shiki shiki-themes github-light github-dark","\u003Cul id=\"menu\">\n    \u003Cli>\u003Ca href=\"#part-1\" class=\"active\">Part 1\u003C\u002Fa>\u003C\u002Fli>\n    \u003Cli>\u003Ca href=\"#part-2\">Part 2\u003C\u002Fa>\u003C\u002Fli>\n    \u003Cli>\u003Ca href=\"#part-3\">Part 3\u003C\u002Fa>\u003C\u002Fli>\n    \u003Cli>\u003Ca href=\"#part-4\">Part 4\u003C\u002Fa>\u003C\u002Fli>\n    \u003Cli>\u003Ca href=\"#part-5\">Part 5\u003C\u002Fa>\u003C\u002Fli>\n\u003C\u002Ful>\n","html","",[36,37,38,65,107,136,165,194,223],"code",{"__ignoreMap":34},[39,40,43,47,51,55,58,62],"span",{"class":41,"line":42},"line",1,[39,44,46],{"class":45},"sVt8B","\u003C",[39,48,50],{"class":49},"s9eBZ","ul",[39,52,54],{"class":53},"sScJk"," id",[39,56,57],{"class":45},"=",[39,59,61],{"class":60},"sZZnC","\"menu\"",[39,63,64],{"class":45},">\n",[39,66,68,71,74,77,79,82,84,87,90,92,95,98,100,103,105],{"class":41,"line":67},2,[39,69,70],{"class":45},"    \u003C",[39,72,73],{"class":49},"li",[39,75,76],{"class":45},">\u003C",[39,78,14],{"class":49},[39,80,81],{"class":53}," href",[39,83,57],{"class":45},[39,85,86],{"class":60},"\"#part-1\"",[39,88,89],{"class":53}," class",[39,91,57],{"class":45},[39,93,94],{"class":60},"\"active\"",[39,96,97],{"class":45},">Part 1\u003C\u002F",[39,99,14],{"class":49},[39,101,102],{"class":45},">\u003C\u002F",[39,104,73],{"class":49},[39,106,64],{"class":45},[39,108,110,112,114,116,118,120,122,125,128,130,132,134],{"class":41,"line":109},3,[39,111,70],{"class":45},[39,113,73],{"class":49},[39,115,76],{"class":45},[39,117,14],{"class":49},[39,119,81],{"class":53},[39,121,57],{"class":45},[39,123,124],{"class":60},"\"#part-2\"",[39,126,127],{"class":45},">Part 2\u003C\u002F",[39,129,14],{"class":49},[39,131,102],{"class":45},[39,133,73],{"class":49},[39,135,64],{"class":45},[39,137,139,141,143,145,147,149,151,154,157,159,161,163],{"class":41,"line":138},4,[39,140,70],{"class":45},[39,142,73],{"class":49},[39,144,76],{"class":45},[39,146,14],{"class":49},[39,148,81],{"class":53},[39,150,57],{"class":45},[39,152,153],{"class":60},"\"#part-3\"",[39,155,156],{"class":45},">Part 3\u003C\u002F",[39,158,14],{"class":49},[39,160,102],{"class":45},[39,162,73],{"class":49},[39,164,64],{"class":45},[39,166,168,170,172,174,176,178,180,183,186,188,190,192],{"class":41,"line":167},5,[39,169,70],{"class":45},[39,171,73],{"class":49},[39,173,76],{"class":45},[39,175,14],{"class":49},[39,177,81],{"class":53},[39,179,57],{"class":45},[39,181,182],{"class":60},"\"#part-4\"",[39,184,185],{"class":45},">Part 4\u003C\u002F",[39,187,14],{"class":49},[39,189,102],{"class":45},[39,191,73],{"class":49},[39,193,64],{"class":45},[39,195,197,199,201,203,205,207,209,212,215,217,219,221],{"class":41,"line":196},6,[39,198,70],{"class":45},[39,200,73],{"class":49},[39,202,76],{"class":45},[39,204,14],{"class":49},[39,206,81],{"class":53},[39,208,57],{"class":45},[39,210,211],{"class":60},"\"#part-5\"",[39,213,214],{"class":45},">Part 5\u003C\u002F",[39,216,14],{"class":49},[39,218,102],{"class":45},[39,220,73],{"class":49},[39,222,64],{"class":45},[39,224,226,229,231],{"class":41,"line":225},7,[39,227,228],{"class":45},"\u003C\u002F",[39,230,50],{"class":49},[39,232,64],{"class":45},[10,234,235,236,239],{},"Here’s the function that checks the user’s current position from the top of the window. Then updates some classes in our menu. Based on different pixel counts, we’ll remove the ",[36,237,238],{},".active"," class and add it to the appropriate list item.",[29,241,245],{"className":242,"code":243,"language":244,"meta":34,"style":34},"language-js shiki shiki-themes github-light github-dark","function scroll_update(){\n\n    \u002F\u002F Get the current window position\n    var position = $(window).scrollTop();\n\n    \u002F\u002F Remove the active state\n    $('#menu li a.active').removeClass('active');\n\n    \u002F\u002F Set li to active based on position\n    if(position \u003C= 500){\n        $('#menu li:eq(0) a').addClass('active');\n    } else if(position \u003C= 1000){\n        $('#menu li:eq(1) a').addClass('active');\n    } else if(position \u003C= 1500){\n        $('#menu li:eq(2) a').addClass('active');\n    } else if(position \u003C= 2000){\n        $('#menu li:eq(3) a').addClass('active');\n    } else{\n        $('#menu li:eq(4) a').addClass('active');\n    }\n}\n","js",[36,246,247,259,265,271,293,297,302,327,332,338,357,379,400,420,438,458,476,496,506,526,532],{"__ignoreMap":34},[39,248,249,253,256],{"class":41,"line":42},[39,250,252],{"class":251},"szBVR","function",[39,254,255],{"class":53}," scroll_update",[39,257,258],{"class":45},"(){\n",[39,260,261],{"class":41,"line":67},[39,262,264],{"emptyLinePlaceholder":263},true,"\n",[39,266,267],{"class":41,"line":109},[39,268,270],{"class":269},"sJ8bj","    \u002F\u002F Get the current window position\n",[39,272,273,276,279,281,284,287,290],{"class":41,"line":138},[39,274,275],{"class":251},"    var",[39,277,278],{"class":45}," position ",[39,280,57],{"class":251},[39,282,283],{"class":53}," $",[39,285,286],{"class":45},"(window).",[39,288,289],{"class":53},"scrollTop",[39,291,292],{"class":45},"();\n",[39,294,295],{"class":41,"line":167},[39,296,264],{"emptyLinePlaceholder":263},[39,298,299],{"class":41,"line":196},[39,300,301],{"class":269},"    \u002F\u002F Remove the active state\n",[39,303,304,307,310,313,316,319,321,324],{"class":41,"line":225},[39,305,306],{"class":53},"    $",[39,308,309],{"class":45},"(",[39,311,312],{"class":60},"'#menu li a.active'",[39,314,315],{"class":45},").",[39,317,318],{"class":53},"removeClass",[39,320,309],{"class":45},[39,322,323],{"class":60},"'active'",[39,325,326],{"class":45},");\n",[39,328,330],{"class":41,"line":329},8,[39,331,264],{"emptyLinePlaceholder":263},[39,333,335],{"class":41,"line":334},9,[39,336,337],{"class":269},"    \u002F\u002F Set li to active based on position\n",[39,339,341,344,347,350,354],{"class":41,"line":340},10,[39,342,343],{"class":251},"    if",[39,345,346],{"class":45},"(position ",[39,348,349],{"class":251},"\u003C=",[39,351,353],{"class":352},"sj4cs"," 500",[39,355,356],{"class":45},"){\n",[39,358,360,363,365,368,370,373,375,377],{"class":41,"line":359},11,[39,361,362],{"class":53},"        $",[39,364,309],{"class":45},[39,366,367],{"class":60},"'#menu li:eq(0) a'",[39,369,315],{"class":45},[39,371,372],{"class":53},"addClass",[39,374,309],{"class":45},[39,376,323],{"class":60},[39,378,326],{"class":45},[39,380,382,385,388,391,393,395,398],{"class":41,"line":381},12,[39,383,384],{"class":45},"    } ",[39,386,387],{"class":251},"else",[39,389,390],{"class":251}," if",[39,392,346],{"class":45},[39,394,349],{"class":251},[39,396,397],{"class":352}," 1000",[39,399,356],{"class":45},[39,401,403,405,407,410,412,414,416,418],{"class":41,"line":402},13,[39,404,362],{"class":53},[39,406,309],{"class":45},[39,408,409],{"class":60},"'#menu li:eq(1) a'",[39,411,315],{"class":45},[39,413,372],{"class":53},[39,415,309],{"class":45},[39,417,323],{"class":60},[39,419,326],{"class":45},[39,421,423,425,427,429,431,433,436],{"class":41,"line":422},14,[39,424,384],{"class":45},[39,426,387],{"class":251},[39,428,390],{"class":251},[39,430,346],{"class":45},[39,432,349],{"class":251},[39,434,435],{"class":352}," 1500",[39,437,356],{"class":45},[39,439,441,443,445,448,450,452,454,456],{"class":41,"line":440},15,[39,442,362],{"class":53},[39,444,309],{"class":45},[39,446,447],{"class":60},"'#menu li:eq(2) a'",[39,449,315],{"class":45},[39,451,372],{"class":53},[39,453,309],{"class":45},[39,455,323],{"class":60},[39,457,326],{"class":45},[39,459,461,463,465,467,469,471,474],{"class":41,"line":460},16,[39,462,384],{"class":45},[39,464,387],{"class":251},[39,466,390],{"class":251},[39,468,346],{"class":45},[39,470,349],{"class":251},[39,472,473],{"class":352}," 2000",[39,475,356],{"class":45},[39,477,479,481,483,486,488,490,492,494],{"class":41,"line":478},17,[39,480,362],{"class":53},[39,482,309],{"class":45},[39,484,485],{"class":60},"'#menu li:eq(3) a'",[39,487,315],{"class":45},[39,489,372],{"class":53},[39,491,309],{"class":45},[39,493,323],{"class":60},[39,495,326],{"class":45},[39,497,499,501,503],{"class":41,"line":498},18,[39,500,384],{"class":45},[39,502,387],{"class":251},[39,504,505],{"class":45},"{\n",[39,507,509,511,513,516,518,520,522,524],{"class":41,"line":508},19,[39,510,362],{"class":53},[39,512,309],{"class":45},[39,514,515],{"class":60},"'#menu li:eq(4) a'",[39,517,315],{"class":45},[39,519,372],{"class":53},[39,521,309],{"class":45},[39,523,323],{"class":60},[39,525,326],{"class":45},[39,527,529],{"class":41,"line":528},20,[39,530,531],{"class":45},"    }\n",[39,533,535],{"class":41,"line":534},21,[39,536,537],{"class":45},"}\n",[10,539,540],{},"Now, we just need to tie this function to the window’s scroll event:",[29,542,544],{"className":242,"code":543,"language":244,"meta":34,"style":34},"$(window).scroll(scroll_update);\n",[36,545,546],{"__ignoreMap":34},[39,547,548,551,553,556],{"class":41,"line":42},[39,549,550],{"class":53},"$",[39,552,286],{"class":45},[39,554,555],{"class":53},"scroll",[39,557,558],{"class":45},"(scroll_update);\n",[10,560,561],{},"That’s it. Now when the user scrolls, your menu will be updated to reflect the current position. We can take this a little further for the sake of preventing the browser from firing our code on every vertical pixel.",[10,563,564,565,571,572,577],{},"Using ",[14,566,570],{"href":567,"rel":568},"http:\u002F\u002Fbenalman.com\u002F",[569],"nofollow","Ben Alman’s"," ",[14,573,576],{"href":574,"rel":575},"http:\u002F\u002Fbenalman.com\u002Fprojects\u002Fjquery-throttle-debounce-plugin\u002F",[569],"Throttle plugin",", we can limit the amount of times our code is fired. Since we’re only checking for change every 500 pixels, we can safely use this approach. First, include the Throttle plugin, then you’ll just need to update the scroll event:",[29,579,581],{"className":242,"code":580,"language":244,"meta":34,"style":34},"$(window).scroll($.throttle(500,scroll_update));\n",[36,582,583],{"__ignoreMap":34},[39,584,585,587,589,591,594,597,599,602],{"class":41,"line":42},[39,586,550],{"class":53},[39,588,286],{"class":45},[39,590,555],{"class":53},[39,592,593],{"class":45},"($.",[39,595,596],{"class":53},"throttle",[39,598,309],{"class":45},[39,600,601],{"class":352},"500",[39,603,604],{"class":45},",scroll_update));\n",[10,606,607],{},"And that’s it. Now our event is only called every 500 milliseconds. This should help performance of your page, even if it may seem insignificant.",[609,610,611],"style",{},"html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s9eBZ, html code.shiki .s9eBZ{--shiki-default:#22863A;--shiki-dark:#85E89D}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":34,"searchDepth":67,"depth":67,"links":613},[],"2011\u002F07\u002F27","There are plenty of sites that have sectioned pages which use an anchored link menu system to jump to each section. If you look back at my smooth anchor post, you can easily adapt that to make the jump behave a little bit nicer. But how about updating the menu so the user is reminded what section they’re on as they scroll down the page? It’s just a few lines of JavaScript+jQuery and you’ll be updating your menu on-the-fly. Let’s get started.","md",{},"\u002Farticles\u002Fupdating-a-menu-based-on-page-location","---\ntitle: Updating a Menu Based on Page Location\ncreatedAt: 2011\u002F07\u002F27\n---\nThere are plenty of sites that have sectioned pages which use an anchored link menu system to jump to each section. If you look back at [my smooth anchor post](\u002Farticles\u002Fsmooth-anchors), you can easily adapt that to make the jump behave a little bit nicer. But how about updating the menu so the user is reminded what section they’re on as they scroll down the page? It’s just a few lines of JavaScript+jQuery and you’ll be updating your menu on-the-fly. Let’s get started.\n\n_Note: Be sure to include a copy of jQuery or this won’t work._\n\nFirst, let’s setup our menu’s HTML:\n\n```html\n\u003Cul id=\"menu\">\n\t\u003Cli>\u003Ca href=\"#part-1\" class=\"active\">Part 1\u003C\u002Fa>\u003C\u002Fli>\n\t\u003Cli>\u003Ca href=\"#part-2\">Part 2\u003C\u002Fa>\u003C\u002Fli>\n\t\u003Cli>\u003Ca href=\"#part-3\">Part 3\u003C\u002Fa>\u003C\u002Fli>\n\t\u003Cli>\u003Ca href=\"#part-4\">Part 4\u003C\u002Fa>\u003C\u002Fli>\n\t\u003Cli>\u003Ca href=\"#part-5\">Part 5\u003C\u002Fa>\u003C\u002Fli>\n\u003C\u002Ful>\n```\n\nHere’s the function that checks the user’s current position from the top of the window. Then updates some classes in our menu. Based on different pixel counts, we’ll remove the `.active` class and add it to the appropriate list item.\n\n```js\nfunction scroll_update(){\n\n\t\u002F\u002F Get the current window position\n\tvar position = $(window).scrollTop();\n\n\t\u002F\u002F Remove the active state\n\t$('#menu li a.active').removeClass('active');\n\n\t\u002F\u002F Set li to active based on position\n\tif(position \u003C= 500){\n\t\t$('#menu li:eq(0) a').addClass('active');\n\t} else if(position \u003C= 1000){\n\t\t$('#menu li:eq(1) a').addClass('active');\n\t} else if(position \u003C= 1500){\n\t\t$('#menu li:eq(2) a').addClass('active');\n\t} else if(position \u003C= 2000){\n\t\t$('#menu li:eq(3) a').addClass('active');\n\t} else{\n\t\t$('#menu li:eq(4) a').addClass('active');\n\t}\n}\n```\n\nNow, we just need to tie this function to the window’s scroll event:\n\n```js\n$(window).scroll(scroll_update);\n```\n\nThat’s it. Now when the user scrolls, your menu will be updated to reflect the current position. We can take this a little further for the sake of preventing the browser from firing our code on every vertical pixel.\n\nUsing [Ben Alman’s](http:\u002F\u002Fbenalman.com\u002F) [Throttle plugin](http:\u002F\u002Fbenalman.com\u002Fprojects\u002Fjquery-throttle-debounce-plugin\u002F), we can limit the amount of times our code is fired. Since we’re only checking for change every 500 pixels, we can safely use this approach. First, include the Throttle plugin, then you’ll just need to update the scroll event:\n\n```js\n$(window).scroll($.throttle(500,scroll_update));\n```\n\nAnd that’s it. Now our event is only called every 500 milliseconds. This should help performance of your page, even if it may seem insignificant.\n",{"title":5,"description":615},"articles\u002Fupdating-a-menu-based-on-page-location","SZAx7R_oYwKLOLK9hgYeMnYn_LOUNuvE00resJfr6eU",1794877726905]