Skip to Content

overflowDetector()

Check if sub-menus are overflowing the viewport and adjust their horizontal positioning accordingly.

JavaScript February 22, 2017

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Additional Notes

This function checks the ID #primarynav for their first sub-menu automatically.

Source File

Located in /assets/js/nebula.js on line 509.

JavaScript
nebula.overflowDetector = function(){
    if ( jQuery('.sub-menu').length ){ //Only add the event listener if sub-menus actually exist
        jQuery('.menu li.menu-item').on({
            'mouseenter focus focusin': function(){
                if ( jQuery(this).children('.sub-menu').length ){ //Check if this menu has sub-menus
                    var submenuLeft = jQuery(this).children('.sub-menu').offset().left; //Left side of the sub-menu
                    var submenuRight = submenuLeft+jQuery(this).children('.sub-menu').width(); //Right side of the sub-menu

                    if ( submenuRight > nebula.dom.window.width() ){ //If the right side is greater than the width of the viewport
                        jQuery(this).children('.sub-menu').addClass('overflowing overflowing-left');
                    } else if (submenuLeft > nebula.dom.window.width() ) {
                        jQuery(this).children('.sub-menu').addClass('overflowing overflowing-right');
                    } else {
                        jQuery(this).children('.sub-menu').removeClass('overflowing overflowing-left overflowing-right');
                    }
                }
            },
            'mouseleave': function(){
                jQuery(this).children('.sub-menu').removeClass('overflowing');
            }
        });
    }
};

Override

To override or disable this JavaScript function, simply redeclare it with the exact same function name.

JavaScript
nebula.overflowDetector = function(){
    //Write your own code here, leave it blank, or return false.
}