R43: How to Highlight Active Links on Scroll and Click for One Page Divi Websites

Next, open up the first section of your first content area, click the Advanced tab and in the CSS ID field, add the same unique name you used in the URL field for the custom link in your menu, but this time without the #.

Then in the CSS Class field, add the class ds-section and then save the section.

How to Highlight Active Links on Scroll and Click for One Page Divi Websites

Next, open the first section of your next content area and repeat the process, using the unique name for that area in the CSS ID field, but the same ds-section class in the CSS Class field.

When you are finished, the first section in each of your content areas should have a unique CSS ID (which is the same as the custom link you created in your menu, but without the #) and the same CSS Class of ds-section.

How to Highlight Active Links on Scroll and Click for One Page Divi Websites

All done? Great, now it’s time for some jQuery.

Here is what we are doing:

To start, we give our first menu item the class ds-menu-active. This is much the same as your home page link on a multipage site being active when you open the site.

Next, we create a variable called scrollPos to store the current vertical position which we will use later in a calculation.

Then we say, for each element with the class ds-section, create another variable called topPos so we can target that section when it is at the top.

Now we use both of the variables we created in a calculation that says, if a section with the class ds-section is equal to, or less than 80px from the top of the page, remove the class ds-menu-active from the previously highlighted item and add the class ds-menu-active to the current item.

  1. jQuery(function ($) {
  2. $(‘#main-header #top-menu li:first-child a, .et_slide_in_menu_container .et_mobile_menu li:first-child a’).addClass(‘ds-menu-active’)
  3. $(window).scroll(function () {
  4. var scrollPos = $(window).scrollTop();
  5. $(‘.ds-section’).each(function (i) {
  6. var topPos = $(this).offset().top;
  7. if ((topPos scrollPos) <= 80) {
  8. $(‘.ds-menu-active’).removeClass(‘ds-menu-active’)
  9. $(‘#main-header #top-menu a:not(li.centered-inline-logo-wrap a), .et_slide_in_menu_container .et_mobile_menu li a’).eq(i).addClass(‘ds-menu-active’)
  10. }
  11. })
  12. });
  13. });

We need to add this code into Divi, but we need to wrap it in script tags for it to work. So copy the complete code from the toggle below and then navigate to Divi > Theme Options > Integration > Add code to the < head > of your blog and paste it in.

If you have a tall main header, for instance when using the centered header format, you may want the your links to change a little earlier. If this is the case then just increase the 80 value in the jQuery until the links change at the point you want.

Complete jQuery
  1. <script>
  2. //Highlight Active Links on Scroll and Click for One Page Divi Websites – By Divi Soup
  3. jQuery(function ($) {
  4. //Set first menu item as active
  5. $(‘#main-header #top-menu li:first-child a, .et_slide_in_menu_container .et_mobile_menu li:first-child a’).addClass(‘ds-menu-active’)
  6. $(window).scroll(function () {
  7. //Create variable to get the current vertical position
  8. var scrollPos = $(window).scrollTop();
  9. //For each section with the class ds-section
  10. $(‘.ds-section’).each(function (i) {
  11. //Create variable to calculate when to add active class
  12. var topPos = $(this).offset().top;
  13. //If the section is equal to or less than 80px from the top of the page
  14. if ((topPos scrollPos) <= 80) {
  15. //Remove the active class from the previous menu item
  16. $(‘.ds-menu-active’).removeClass(‘ds-menu-active’)
  17. //Add the class to the current menu item
  18. $(‘#main-header #top-menu a:not(li.centered-inline-logo-wrap a), .et_slide_in_menu_container .et_mobile_menu li a’).eq(i).addClass(‘ds-menu-active’)
  19. }
  20. })
  21. });
  22. });
  23. </script>

Now for a little CSS.

Our jQuery applies a CSS Class of ds-menu-active to our current/active menu item, but we need to add some styles to that class to change the appearance of that menu item.

All we are doing here is applying a different colour, but you can add in any styling you wish.

We use two selector strings. The first styles menu items when using the default, centered and centered inline header formats (including vertical navigation), and the second when using the slide-in and fullscreen header formats.

  1. #main-header #top-menu a.ds-menu-active,
  2. .et_slide_in_menu_container .et_mobile_menu a.ds-menu-active {
  3. color: #FE437D !important;
  4. }

Copy the CSS above and paste into your child theme stylesheet or Divi > Theme Options > Custom CSS.