If the number of items in the Horizontal Menu increases, it should be able to scroll left and right

I am facing an issue with the horizontal menu on my website, as it extends beyond the page width, creating a horizontal scroll bar which looks unappealing.

The menu is dynamic and if the number of items (options in the menu) increases, it overflows the boundary. I am looking to implement a jQuery solution where the menu displays icons (left and right arrows) when the number of items increases. Clicking on these icons should scroll the menu left and right.

Below is the HTML structure:

<div class="wrapperClass">
<div id="mainContainerClass">
<!-- Horizontal Menu Starts Here -->
    <div id="menuContainer">
    <ol id="menuList">
      <li><div id="popupmenu"><a  href="page1.html" ><span>Menu Item 1</span></a></div>  </li>
      <li><div id="popupmenu"><a  href="page2.html" ><span>Menu Item 2</span></a></div>  </li>
      ...
    </div>
</div>

CSS styling for the menu:

.wrapperClass { width:1000px;   margin: 0px auto;   padding:0px;}
...
#popupmenu div a:hover  {   background: #11439d;        color: #FFF;}

Next, there is a second example of a different menu structure:

    <div id="secondMenue" >
<a href="page1a.html" class="othermenu">Sec Item 1</a>  | 
<a href="page1b.html" class="active">Sec Item 2</a>  |
...
</code></pre>

<p>In a specific layout that cannot be changed, a carousel needs to be applied. The layout is shown below:</p>

<pre><code>'<div>
    <table>
        <tr>
            <td><table><tr><td><a href="#"><span>Menu Option 1</span></a></td></tr></table></td>
            <td><table><tr><td><a href="#"><span>Menu Option 2</span></a></td></tr></table></td>
            ...
        </tr>
    </table>
  </div>' 

Answer №1

A code submission is necessary for you to proceed. Consider using the following CSS code:

overflow:hidden

This will prevent scroll bars from appearing on the page.

For a visual example of what you're trying to achieve, check out this JS fiddle demo based on my understanding. You can also view the code here.

I implemented two CSS cases for the menu to switch between using jQuery's windows.resize()

CSS

body {font-family:Arial, Helvetica, sans-serif; font-size:12px;}

.container {
  margin-right: auto;
  margin-left: auto;
  padding-left: 15px;
  padding-right: 15px;
}

.menu {width:100%; overflow:auto;list-style: none; height:32px; white-space:nowrap}
.menu li {height:32px; background:#eee; margin-left:5px; padding:5px; float:left }
.menu.text li {width:200px;}
.menu.icons li {width:32px; background:#ffcc00}
.menu.icons li a{text-indent: -9999px; display:inline-block}

@media (min-width: 768px) {
  .container {
    width: auto;
  }
}
@media (min-width: 992px) {
  .container {
    width: 970px;
  }
}
@media (min-width: 1200px) {
  .container {
    width: 1170px;
  }
}

jQuery

$(function () {
    var win = $(window);

    win.bind(
    'resize',
    function () {
        setMenu();
    }
).trigger('resize');

});

    function setMenu() {
        var _menu = $('#menu').width();
        var _width = (Math.floor(_menu));
        if (_width < 1025) {
            $('#menu').removeClass("text").addClass("icons");
        } else {
            $('#menu').removeClass("icons").addClass("text");
        }
    }

HTML

<div class="container">
    <ul id="menu" class="menu text">
        <li class="nav">
            <a class="navtab" href="#">Item one</a>
        </li>
        <li class="nav">
            <a class="navtab" href="#">Item two</a>
        </li>
        <li class="nav">
            <a class="navtab" href="#">Item three</a>
        </li>
        <li class="nav">
            <a class="navtab" href="#">Item four</a>
        </li>
        <li class="nav">
            <a class="navtab" href="#">Item five</a>
        </li>
        <li class="nav">
            <a class="navtab" href="#">Item six</a>
        </li>
        <li class="nav">
            <a class="navtab" href="#">Item seven</a>
        </li>
        <li class="nav">
            <a class="navtab" href="#">Item eight</a>
        </li>
        <li class="nav">
            <a class="navtab" href="#">Item nine</a>
        </li>
        <li class="nav">
            <a class="navtab" href="#">Item ten</a>
        </li>
    </ul>

Alternative Solution

You can also consider adding a jQuery carousel. Here's a carousel demo that complements your code example.

Update:

If you have no further concerns, you can utilize RenderMode to switch the ASP.NET menu from a table to a list format.

<asp:Menu ID="YourMenu" runat="server" Orientation="Horizontal" CssClass="menu" RenderingMode="List" >

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for aligning a custom icon to the left in a Jquery mobile header

I have successfully implemented a custom icon in the header, but it is currently displaying in the center. How can I adjust it to be positioned on the left side instead? <div data-role="header" data-theme="a"> <h3> ...

I am looking to have my page refresh just one time

I have created an admin page where I can delete users, but each time I delete a user, the page requires a refresh. I attempted to use header refresh, but this action caused my page to refresh multiple times. Is there a way to ensure that my page only refr ...

Tips on utilizing Jquery to extract an array containing UL, LI, and input elements

I am currently working with five ul lists, each of which may contain an input element. For example: <form id="testForm"> <ul id="1"> <li>TEST</li> <li>Gender: <select id="gender" name="gender"> ...

Issue with md-stretch-tabs in Angular-Material causing a problem

I am in the process of developing an Angular-based application. ISSUE: I included md-stretch-tabs in my md-tabs element, but when I switch to tab#2, the tab retracts as if there is not enough space to flex. Is this problem related to dependencies or the c ...

What is the process for updating the ID within a Select2 ajax script function?

I am facing an issue with my select2 ajax script. The unique_ID in my table field is named "no", which is causing me to be unable to select an item in Select2 drop down. However, when I changed the name of my database table field from "no_donatur" to "ID", ...

What is the best way to convert my JSON data to HTML using JavaScript?

My dilemma seems to be rooted in the use of backticks. Strangely, an error keeps popping up whenever I try to employ them in my IDE (Brackets). I attempted solutions by testing directly in my Browser and through NotePad++, but unfortunately, nothing seeme ...

Customize the MUI Slider Component with unique colored thumbs

Check out MUI's slider component at this link. I'm using it to let users select a value range with two thumbs. You can find the documentation for MUI's multi-thumb slider here. If you want to customize the style of the slider thumb, visit ...

adjust division size proportionally to another one

I am trying to create a layout with right and left divisions where the size of the left division matches the height of the browser window (100vh). The left division consists of two sub-divisions that need to have variable sizes but collectively match the ...

Revolutionary AJAX technology seamlessly updates and refreshes elements within every row or form on a webpage

<script type="text/javascript"> $(document).ready(function() { $("#submit<?php echo $resultshome['hskid']; ?>").click(function() { $.ajax({ type : "POST", url : "/scores/printPOST.php", data : { "subro ...

Is there a way to code a checkbox within a dropdown menu?

I am seeking assistance in making this dynamic I am still learning Laravel and require some guidance on how to program a checkbox within a select dropdown. Here is the HTML form I have: <div class="container w60"> <div class="row pad_y_2 ...

Showing MYSQL data in an html table

Hey there! I've been working on a website that features HTML5 video player to play videos. I have a "watch" page that retrieves data from the database based on the id parameter (watch.php?v=1). Now, I'm looking to display the most recent videos o ...

Validate Forms with JQuery Validator Plugin and Ajax

Recently, I started using the JQuery Form Validator, and I am still learning about Ajax. The validation is working well, but I have encountered an issue where the form is still submitted even if there are errors present. <form method="post" id="cont ...

Utilize Google Spreadsheet to choose a specific worksheet using JSONP

Exploring the First endpoint I have been delving into the details of the Google query language specification . When trying to select a specific page, I encountered some challenges. How can one select a page through this endpoint? I attempted the followin ...

The issue of jQuery's .last() function triggering multiple times with just a single click on the last

I currently have a set of tabs containing radio buttons and I need to trigger an event when the last option is selected (meaning any radio button within the final tab, not just the last radio button). Below is the HTML code: <div id="smartwizard" clas ...

What methods can I employ to utilize preg_match with jQuery?

Is it possible to validate a phone number in jQuery using preg_match? I have tried the following code without success: if (!preg_match("/^[0-9]{3}-|\s[0-9]{3}-|\s[0-9]{4}$/", phone.val() )) { phone.addClass("needsfille ...

The height of the iframe with the id 'iframe' is not functioning as expected

I am trying to set the iFrame's height to 80% and have an advertisement take up the remaining 20%. Code <!DOCTYPE html> <html lang="en"> <head> </head> <body> <iframe id="iframe" src="xxxxxx" style="border: ...

Creating a Basic Slideshow using Javascript

I've been attempting to create a slideshow using only JavaScript, but I've run into an issue. When I set the divs to change on click, it works perfectly fine. However, when I try to set it to change at specific intervals, it doesn't work. ...

Preventing scrolling on a YouTube video embed

I have been working hard on developing my personal website. One issue I encountered is that when there is a YouTube video embedded in the site, it stops scrolling when I hover over it. I would like the main document to continue scrolling even if the mouse ...

Notification during image loading

I have successfully integrated the Nivo slider on my website, however, I am encountering a minor issue. Whenever the images are loading, it causes the footer to shift upwards which affects the overall look of the site. Is there a simple solution to add a m ...

Send the selected option from the dropdown menu to a different page

I am in the process of designing a unique shopping cart system where, upon clicking on the "add to cart" link, both the selected quantity from the drop-down menu and the product ID are transmitted to the addCart page. While I have successfully managed to p ...