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 8020705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:45:43+00:00 2026-06-04T21:45:43+00:00

I have a basic jQuery dropdown menu and am testing it on mobile devices,

  • 0

I have a basic jQuery dropdown menu and am testing it on mobile devices, specifically phones.

One issue I’ve encountered is that submenus on the right side sometimes go off the edge of the screen, because the text in one or more of the items is too long. The user can, of course, scroll slightly to the right, but that’s not a great solution. There is a similar question here, but I couldn’t get the proposed answers to work (plus I’m fumbling around since I’m still totally new to JS/jQuery).

The general idea seems to be to get the left position+width of the current submenu (making sure they are shown first, then hide them again or show them and shift them when the user taps the menu) and see if that value is greater than the screen width. If it is, then it should be shifted to the left by the difference. However, my attempts don’t seem to do anything to the elements and the menus keep appearing partially off-screen. Currently, I have this (runs after the body loads):

var menus = $('#navbar').find('.subMenu');
menus.show();
menus.each(function(index) {
  var offsetMenu = $(this).offset();
  var diff = screen.width - ($(this).outerWidth + offsetMenu.left);
  if(diff < 0) {
    $(this).offset({top:offsetMenu.top, left:offsetMenu + diff});
  }
  });
menus.hide();

The jQuery version is 1.7.2 and the HTML structure is as follows (simplified version):

<div id="navbar">
  <ul>
    <li class="dropdown"><a href="#">Link A</a></li>
    <li class="dropdown"><a href="#">Link B</a>
      <ul class="subMenu">
        <li><a href="#">Link 1</a></li>
        <li><a href="#">Link 2</a></li>
      </ul>
    </li>
  </ul>
</div>

How can I get this to work?

Edit: Here’s the relevant CSS, too.

#navbar ul.subMenu {
    position:absolute;
    display:none;
    background-color:#FFAF1E;
}
#navbar ul.subMenu li {
    clear:both;
}
#navbar a {
    text-decoration:none;
    color:#0C1F6E;
    display:block;
    padding-right:10px;
    padding-left:10px;
}
  • 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-06-04T21:45:45+00:00Added an answer on June 4, 2026 at 9:45 pm

    I finally got the following to work:

    function adjustSubMenus()
      {
          var menus = $('#navbar').find('.subMenu');
          menus.each(function(){
              $(this).css("left","");
              adjustSingleMenu($(this))
              } );
      }
      function adjustSingleMenu(element)
      {
          element.show();
          var thisMenuWidth = element.outerWidth();
          var thisMenuPos = element.offset();
          var diff = (thisMenuWidth + thisMenuPos.left) - window.outerWidth;
          if(diff > 0) {
              element.css("left", function() { return thisMenuPos.left - diff; });
          }
          element.hide();
      }
      $('body').ready(function() {
          $(window).resize(adjustSubMenus());
          if(window.outerWidth <= 720) {
              $('html').mousedown(function() {
                  $('#navbar').find('.subMenu').each(function(index) { 
                    if($(this).is(":visible") ) {
                        $(this).css("left","");
                        $(this).hide(); 
                        }
                  } );
              });
    
              $('#navbar').mousedown(function(event) {
                  event.stopPropagation();
              });
    
              $('.dropdown').mousedown(function() {
                  var ele = $(this).find('.subMenu');
                  $('#navbar').find('.subMenu').each(function(index) { 
                    if(!$(this).is(ele) && $(this).is(":visible") ) {
                        $(this).css("left","");
                        $(this).hide(); 
                        }
                  } );
                  if(ele.is(":visible")) {
                      ele.css("left","");
                      ele.hide();
                  }
                  else {
                      adjustSingleMenu(ele);
                      ele.show();                 
                  }
                  });
          }
          else
          {
              $('.dropdown').hover(
                  function() { $(this).find('.subMenu').stop(true,true).slideDown("fast"); },
                  function() { $(this).find('.subMenu').stop(true,true).hide(); } 
              );
          }
      });
    

    The only minor caveat is that if you have a menu open as you change the orientation of the phone, the menu may appear off the screen. Closing and reopening the menu will fix this issue. Everything else works perfectly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this extremely basic jQuery UI Modal Dialog that I wrote for testing
I have a very basic jquery mobile modal dialog that INTERMITTENTLY reloads on clicking
I have a pretty basic jquery mobile page, it's 3 data-role:page divs in one
I'm looking to implement a fairly basic jQuery slideshow. One that automatically loops through
I have a basic jquery-ui dialog widget that is set to execute on a
I am trying to build a basic drop down menu with jquery. I have
I currently have a very basic JQuery script accompanied by some CSS that helps
I have a basic JsonResult method that is being called by a jQuery $.ajax
I have written a very basic jquery script that is true theorically but not
I have a basic jQuery webpage that shows off some basic features, however there

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.