Advertisement
danstramer

Divi one page active menu item by section

Jan 2nd, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2. $(document).ready(function() {
  3.     var navChildren = $("#top-menu li").children();
  4.     var aArray = [];
  5.     for (var i = 0; i < navChildren.length; i++) {
  6.         var aChild = navChildren[i];
  7.        // var ahref = $(aChild).attr('href');
  8.        
  9. var ahrefTMP = $(aChild).attr('href');
  10. var ahref = ahrefTMP.substring(ahrefTMP.indexOf('#'));
  11.         aArray.push(ahref);
  12.     }
  13.     $(window).scroll(function() {
  14.         var windowPos = $(window).scrollTop();
  15.         var windowHeight = $(window).height();
  16.         var docHeight = $(document).height();
  17.         for (var i = 0; i < aArray.length; i++) {
  18.             var theID = aArray[i];
  19.             var secPosition = $(theID).offset().top;
  20.             secPosition = secPosition - 135;
  21.             var divHeight = $(theID).height();
  22.             divHeight = divHeight + 90;
  23.             if (windowPos >= secPosition && windowPos < (secPosition + divHeight)) {
  24.                 $("a[href='" + theID + "']").parent().addClass("active");
  25.             } else {
  26.                 $("a[href='" + theID + "']").parent().removeClass("active");
  27.             }
  28.         }
  29.     });
  30.  
  31. });
  32. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement