Skip to Content
Menu

overflowDetector()

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

JavaScript February 7, 2021

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.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/helpers.js on line 88.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.overflowDetector = async 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
                        let submenuLeft = jQuery(this).children('.sub-menu').offset().left; //Left side of the sub-menu
                        let 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.
    }