Skip to content

Instantly share code, notes, and snippets.

@mrwweb
Created December 16, 2016 22:46
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 mrwweb/6063644df6b747dd55b839683edef048 to your computer and use it in GitHub Desktop.
Save mrwweb/6063644df6b747dd55b839683edef048 to your computer and use it in GitHub Desktop.
Some nice little tweaks to the default superclick init
(function($){ //create closure so we can safely use $ as alias for jQuery
"use strict";
$(document).ready(function(){
var mainMenu = $('#primary-menu').superclick({
activeClass: 'sf-active', // change active class from sfHover to sf-active
cssArrows: false, // no arrows (but you should probably provide your own)
speed: 1, // "no animation"
onShow: function() {
// keep off screen momentarily
$(this).css('top','-9999px');
// calculate position of submenu
var winWidth = $(window).width();
var outerWidth = $(this).outerWidth();
var rightEdge = $(this).offset().left + outerWidth;
// if difference is greater than zero, then
if( rightEdge > winWidth ) {
// You must add this CSS to a stylesheet somewhere!
// .submenu--right { left: auto; right: 0; }
$(this).addClass('sub-menu--right');
}
// remove top value
$(this).css('top','');
},
onHide: function() {
$(this).removeClass('sub-menu--right');
}
});
// close all superfish menus when closing _s menu toggle
$('#menu-toggle').on( 'click', function() {
mainMenu.children('.sf-active').superclick('hide')
});
// close superfish menu when hitting escape. return focus to parent menu item link
$('.sf-menu .sub-menu').keyup(function(event){
if (event.keyCode == 27){
// Close the modal/menu
mainMenu.superclick('hide');
// Return focus to the element that invoked it
$(this).parent().children('a').focus();
}
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment