Skip to content

Instantly share code, notes, and snippets.

@RanMetser
Created December 30, 2016 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RanMetser/e98ed927fa10c20d97cd6ee8a1063f2b to your computer and use it in GitHub Desktop.
Save RanMetser/e98ed927fa10c20d97cd6ee8a1063f2b to your computer and use it in GitHub Desktop.
Fix Pojo menu active mode for a one page menu
jQuery(document).ready(function(jQuery) {
var topMenu = jQuery("#menu-main"),
// change offset manually as needed (usually menu height)
offset = 120,
topMenuHeight = topMenu.outerHeight()+offset,
menuItems = topMenu.find('a[href*="#"]'),
// Anchors corresponding to menu items
scrollItems = menuItems.map(function(){
var href = jQuery(this).attr("href"),
id = href.substring(href.indexOf('#')),
item = jQuery(id);
//Home anchor activates on page load
menuItems.parent().end().filter("[href*='#home']").parent().addClass("active");
if (item.length) { return item; }
});
// Bind to scroll
jQuery(window).scroll(function(){
// Get container scroll position
var fromTop = jQuery(this).scrollTop()+topMenuHeight;
// Get id of current scroll item
var cur = scrollItems.map(function(){
if (jQuery(this).offset().top < fromTop)
return this;
});
// Get the id of the current element
cur = cur[cur.length-1];
var id = cur && cur.length ? cur[0].id : "";
menuItems.parent().removeClass("active");
if(id){
menuItems.parent().end().filter("[href*='#"+id+"']").parent().addClass("active");
}
//Home anchor activates near top
if((jQuery(this).scrollTop()) <= offset) {
menuItems.parent().end().filter("[href*='#home']").parent().addClass("active");
}
})
})
@RanMetser
Copy link
Author

RanMetser commented Dec 30, 2016

This code fixes an issue with Pojo one-page menu (using anchors).

When using the Pojo Elementor page builder to design the page, menu items will not dynamically turn the "active" mode on and off when scrolling the page.
See following discussion thread (Hebrew):
http://pojo.co.il/topic/%D7%A9%D7%99%D7%A0%D7%95%D7%99-%D7%A6%D7%91%D7%A2-%D7%AA%D7%A4%D7%A8%D7%99%D7%98-%D7%9C%D7%A4%D7%99-%D7%94%D7%9E%D7%99%D7%A7%D7%95%D7%9D-%D7%91%D7%93%D7%A3-one-pager/

Please note:

  • Anchor name must be in English;
  • Anchor name must use relative URL (e.g. #home);
  • Lines 13 and 39 include a hard coded string, #home. This is for the first item on the menu list. You can update if using a different one.
  • The only variable which should be adjusted manually is 'offset'. Usually you'd want this to equal the menu height;
  • Always use a child theme. Save the code as a .js file and enqueue it properly in your child theme functions.php file.

[Note: The script is a compilation of several scripts found online, from different sources, updated so it works with Pojo theme and Elementor.]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment