Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7700205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:39:17+00:00 2026-05-31T22:39:17+00:00

I have built a drop-down menu here http://dev.driz.co.uk/jsonmenu/ that uses JSON to dynamically build

  • 0

I have built a drop-down menu here http://dev.driz.co.uk/jsonmenu/ that uses JSON to dynamically build the menus. The reason for doing this insead of putting the HTML in the page itself is for performance (only load what you need) and so that I can position the menu outside of the table.

I have begun implementing a way to position the menu based on its height and proximity to the window (browser window NOT the scrollable container). The problem is that when the height of the menu is bigger than the space below the link and viewport then it should move the menu above and vice-versa. Parts of it are working just not quite correctly.

NOTE: The reason the positioning is done after the HTML is inserted rather than in a callback if user moves the window etc is because the menu is removed if the user scrolls or anything like that so it would be pointless to do it then.

Code as requested:

        $(document).ready(function () {

            $('a.buildMenu').click(function (event) {

                // Prevent normal behaviour
                event.preventDefault();

                // Stops it bubbling to the document
                event.stopPropagation();

                var link = $(this);

                // If the menu already exists remove it and exit the request
                if($(document).find('div#' + $(link).data('id')).length){
                    $('.buildMenu').removeClass('selected');
                    $('.menu').remove();
                    return false;
                }

                // Remove any other menus from the DOM
                $('.buildMenu').removeClass('selected');
                $('.menu').remove();

                // Get the position of the link
                var offset = link.offset();
                var top = offset.top;
                var left = offset.left; 
                var bottom = top + link.height();
                var right = $(window).width() - link.width();

                top = top + link.height();

                bottom = bottom + link.height();

                // Append the menu to the DOM in position
                var menuInstance = $('<div class="menu loading">loading...</div>').appendTo('body').css({'position':'absolute','top':top,'left':left});

                // Add the instance id
                $(menuInstance).attr('id', $(link).data('id'));

                // Add class of selected to clicked link
                $(this).addClass('selected');

                // Request JSON data        
                $.ajax({
                    url: 'menu.json',
                    timeout: 5000,
                    dataType: 'JSON',
                    success: function (data) {

                        // Build the menu
                        var ul = $("<ul/>").attr("id", data.menu.id).addClass(data.menu.class);

                        // For each menu item
                        $.each(data.menu.content.menuitem, function () {
                            var li = $("<li/>").appendTo(ul).addClass(this.liClass);
                            var anchor = $("<a/>").appendTo(li).attr("href", this.href).attr("title", this.title);
                            var span = $("<span/>").appendTo(anchor).addClass(this.icon).html(this.text)
                        });

                        // Remove the loading class and insert menu into instance
                        $(menuInstance).removeClass('loading').html(ul);

                        // If the menu is taller than the bottom space
                        if(menuInstance.height() > bottom) {
                            menuInstance.css({'top':'auto','bottom':bottom,'left':left});
                        }
                        // If the menu is taller than the top space
                        else if(menuInstance.height() > top) {
                            menuInstance.css({'top':top,'left':left});
                        }
                        // Default position...
                        else {
                            menuInstance.css({'top':top,'left':left});
                        }

                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        console.log(jqXHR, textStatus, errorThrown);
                    }
                });

            });

            // Remove menu from DOM if anything except the menu is clicked
            $(document).bind('click', function (event) {
                var clicked = $(event.target);
                if (!clicked.parents().hasClass('menu')) {
                    $('.buildMenu').removeClass('selected');
                    $('.menu').remove();
                }
            });

            // Remove menu if user scrolls panel
            $(window).bind('resize', function() {   
                $('.buildMenu').removeClass('selected');
                $('.menu').remove();
            });

            $('div').bind('scroll', function() {    
                $('.buildMenu').removeClass('selected');
                $('.menu').remove();
            });

        });

Here is a screenshot of the issue (the code should make that menu appear above the link because its height is bigger than the offset space below the link)

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:39:18+00:00Added an answer on May 31, 2026 at 10:39 pm

    Looks like the problem is with Line 71-74.

    // If the menu is taller than the bottom space
    if(menuInstance.height() > bottom) {
          menuInstance.css({'top':'auto','bottom':bottom,'left':left});
    }
    

    bottom is being calculated as the distance from the top of the window to bottom of the menu link … and you’re saying “if the menu height is bigger than that”.

    I think what you’re wanting to check is the if the menu height is bigger than the distance from the bottom of the menu link to the bottom of the window. So…

        if(menuInstance.height() > ($(window).height() - bottom) )
    

    This should take the distance from the bottom of the link to the bottom of the window and compare that to the height of the menu.

    Then you need to correct how you’re position menuInstance.

        'bottom': ($(window).height() - top) + link.height()
    

    Here’s the full code…

        // If the menu is taller than the bottom space
        if (menuInstance.height() > ($(window).height() - bottom)) {
            menuInstance.css({
                'top': 'auto',
                'bottom': ($(window).height() - top) + link.height(),
                'left': left
            });
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have built jQuery drop-down menu which is having problems floating over the UI
I am trying to build a drop-down menu that will show a row to
I am trying to build a basic drop down menu with jquery. I have
I'm trying to build a css drop down menu. I have a div #category_list
I have to build a form where I want a drop down menu list,
I have built an application that uses SQL Express 2005 and I want to
I have two ajax functions that fetch JSON objects using .getJSON and then build
I'm using spark List s and PopupAnchor s to build a drop down menu
I've built a dropdown menu that uses a slideUp event if the menu itself
I have built a dropdown/ mega menu that works perfectly when placed in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.