NOTE: It seems that you took this code from my answer to this SO question , I edited it to cover your case. Other people looking for more code can check it out for a snippet .
So you have two problems:
- The last item is not highlighted.
- When you click on a menu item, the page scrolls to the corresponding section, but this menu does not stand out if you scroll the page a bit.
Problem 1
It's easy, you just forgot to add the id attribute in the last section :)
It should be:
<section id="contact" class="container contact-us section">
Problem 2
Your click event triggers a scroll animation in the appropriate section, but since the navigation bar is at the top of the page, you made the animation so that it leaves a small box at the top. This field prevents the section from getting to the top of the page, so the menu item is not highlighted.
@Shnibble pointed you in the right direction, you can add a small positive margin to the value returned by $(window).scrollTop() (or negative to offset().top element).
So, following the code you included, it will be something like:
if (position + my_margin >= target) {
Fields can be the height of your navigation bar:
my_margin = $('#site-navigation').height();
You can obviously add a little more or less to adapt it to your needs.
source share