Skip to content

Instantly share code, notes, and snippets.

@colorful-tones
Last active August 29, 2015 14:23
Show Gist options
  • Save colorful-tones/da7599eb8dc186cca91d to your computer and use it in GitHub Desktop.
Save colorful-tones/da7599eb8dc186cca91d to your computer and use it in GitHub Desktop.
( function($) {
var menuToggle = $('.menu-toggle');
var overlay = $('.overlay');
var closeButton = $('.overlay-close');
var searchToggle = $('.search-toggle');
var searchForm = $('.search-form');
var ww = window.innerWidth;
$(document).ready(function() {
$('.menu li a').each(function() {
if ($(this).next().length > 0) {
$(this).addClass('parent');
};
})
menuToggle.click(function() {
overlay.toggleClass('open');
});
closeButton.click(function() {
if(overlay.hasClass('open')) {
overlay.removeClass('open');
}
});
searchToggle.click(function() {
searchForm.toggle();
$('.main-menu').toggleClass('open');
console.log('clicked');
});
adjustMenu();
adjustSearch();
})
$(window).bind('resize orientationchange', function() {
ww = window.innerWidth;
adjustMenu();
adjustSearch();
});
var adjustMenu = function() {
if (ww < 760) {
// if "more" link not in DOM, add it
if ( !$('.menu .more-toggle')[0] ) {
$('<div class="more-toggle">&nbsp;</div>').insertBefore($('.parent:not(.search-toggle)'));
}
$('.overlay li').unbind('mouseenter mouseleave');
$('.overlay li a.parent').unbind('click');
$('.overlay li .more-toggle').unbind('click').bind('click', function() {
$(this).parent('li').toggleClass('hover');
});
} else if (ww >= 760) {
// remove .more link in desktop view
$('.menu .more-toggle').remove();
overlay.show();
$('.overlay li').removeClass('hover');
$('.overlay li a:not(.search-toggle)').unbind('click');
$('.overlay li').unbind('mouseenter mouseleave').bind('mouseenter mouseleave', function() {
// must be attached to li so that mouseleave is not triggered when hover over submenu
$(this).toggleClass('hover');
});
}
}
var adjustSearch = function() {
if (ww >= 760 && ww <= 1280) {
$('.main-menu').append(searchForm.hide());
if ( !$('.search-form input[type="search"]').attr("placeholder", "Type your search here") ) {
$('.search-form input[type="search"]').attr("placeholder", "Type your search here");
}
}
}
} )(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment