Skip to content

Instantly share code, notes, and snippets.

@rebdev
Last active December 8, 2015 07:04
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 rebdev/7e15433cf637696de0a8 to your computer and use it in GitHub Desktop.
Save rebdev/7e15433cf637696de0a8 to your computer and use it in GitHub Desktop.
Variation on a WordPress accordion menu
/*
* Variation on a WordPress accordion menu from the tutorial at http://cssmenumaker.com/blog/wordpress-accordion-menu-tutorial
* but
* a) with parent menu item links no longer removed when clicked, and
* b) with active items' immediate submenues and parents open by default.
*
* Original tutorial version by Russell Martin. Variations by Rebecca Appleton.
*
* NB: Tutorial URL above has other files downloadable that are required along with this one for this to work.
*/
( function( $ ) {
$( document ).ready(function() {
// For any active item, show its immediate children and its ancestors as 'open' by default.
$('#cssmenu li.current_page_item, #cssmenu li.current-menu-parent, #cssmenu li.current_page_ancestor').each(function() {
$(this).addClass('open');
$(this).children('ul').css("display", "block");
});
// When you click on a link that has a submenu
// a) if it's the active item, don't follow the link, just show its children and close its siblings.
// b) if it's not the active item, then just follow the link.
$('#cssmenu li.has-sub>a').on('click', function(event){
var element = $(this).parent('li');
if (element.hasClass('active') ) {
event.preventDefault();
}
// If the parent li already has the class open, we are closing this menu item.
// Remove the 'open' class, also remove the 'open' class from any of its children.
if (element.hasClass('open')) {
element.removeClass('open');
element.find('li').removeClass('open');
// Do slide-up animation
element.find('ul').slideUp();
} else {
// The parent li doesn't have the class 'open', so we are opening it.
// Add the class 'open' to it and do the slide-down animation.
element.addClass('open');
element.children('ul').slideDown();
// Now for its siblings' submenues...
// Slide up and remove the 'open' class from any of its siblings' immediate children.
element.siblings('li').children('ul').slideUp();
element.siblings('li').removeClass('open');
// Slide up and remove the 'open' class from any of its siblings' children of all levels.
element.siblings('li').find('li').removeClass('open');
element.siblings('li').find('ul').slideUp();
}
});
$('#cssmenu>ul>li.has-sub>a').append('<span class="holder"></span>');
(function getColor() {
var r, g, b;
var textColor = $('#cssmenu').css('color');
textColor = textColor.slice(4);
r = textColor.slice(0, textColor.indexOf(','));
textColor = textColor.slice(textColor.indexOf(' ') + 1);
g = textColor.slice(0, textColor.indexOf(','));
textColor = textColor.slice(textColor.indexOf(' ') + 1);
b = textColor.slice(0, textColor.indexOf(')'));
var l = rgbToHsl(r, g, b);
if (l > 0.7) {
$('#cssmenu>ul>li>a').css('text-shadow', '0 1px 1px rgba(0, 0, 0, .35)');
$('#cssmenu>ul>li>a>span').css('border-color', 'rgba(0, 0, 0, .35)');
}
else
{
$('#cssmenu>ul>li>a').css('text-shadow', '0 1px 0 rgba(255, 255, 255, .35)');
$('#cssmenu>ul>li>a>span').css('border-color', 'rgba(255, 255, 255, .35)');
}
})();
function rgbToHsl(r, g, b) {
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if(max == min){
h = s = 0;
}
else {
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return l;
}
});
} )( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment