Hidden overflow causing nav-tabs to malfunction

I developed a bootstrap 4 nav-tabs bar with separate "main tabs" and "sub tabs" that slide out from the main tabs using jquery's animate() function. Check out the code snippet below:

CSS:

.nav-tabs .nav-item.main-item{
    border-radius: 0px;
    background: white;
}
.nav-tabs .nav-item.sub-item {
    background: rgba(240,240,240,1);
    margin-left: -5px;
}

HTML:

<ul class="nav nav-tabs">
    <li class="nav-item main-item"><a class="nav-link active" data-toggle="tab" href="#main1"><b>Main 1</b></a></li>
    <li class="nav-item main-item"><a class="nav-link" data-toggle="tab" href="#main2"><b>Main 2</b></a></li>
    <li class="nav-item sub-item" main="#main1"><a class="nav-link" data-toggle="tab" href="#sub1">Sub 1</a></li>
    <li class="nav-item sub-item" main="#main1"><a class="nav-link" data-toggle="tab" href="#sub2">Sub 2</a></li>
    <li class="nav-item sub-item" main="#main1"><a class="nav-link" data-toggle="tab" href="#sub3">Sub 3</a></li>
    <li class="nav-item sub-item" main="#main1"><a class="nav-link" data-toggle="tab" href="#sub4">Sub 4</a></li>
    <li class="nav-item sub-item" main="#main1"><a class="nav-link" data-toggle="tab" href="#sub5">Sub 5</a></li>
    <li class="nav-item main-item"><a class="nav-link" data-toggle="tab" href="#Main 3"><b>Plan</b></a></li>
    <li class="nav-item main-item"><a class="nav-link" data-toggle="tab" href="#Main 4"><b>Simulate</b></a></li>
</ul>

JS: (jQuery)

$(".nav-tabs .nav-item.main-item").click(function (){
    var hr = $(this).find(".nav-link").attr("href");
    $(".nav-tabs .nav-item.sub-item:not([main="+hr+"])").stop().animate({width: 'hide'});
    $(".nav-tabs .nav-item.sub-item[main="+hr+"]").stop().animate({width: 'show'});
});

While it functions correctly, I noticed that during the animation, the bar becomes distorted due to the "overflow: hidden" property set by jQuery for the animation. Refer to the images below:

Expanded: Nav-tabs sub items fully expanded

During Animation: Nav-tabs sub items during animation

It seems like the overflow: hidden introduces some hidden margins that push down other elements (the main tabs). I have tried various solutions, such as removing margins and setting elements to display: inline-block, but unfortunately, the issue persists.

I have spent several days attempting to rectify this problem without success, so I am hopeful that someone can provide assistance.

EDIT: Some individuals recommended adding overflow:hidden in the CSS to the sub-tabs. However, this resulted in the expanded state also being affected. For reference, please view the following example: Messed up expanded state

Answer №1

UPDATED

New JavaScript Code:

$(function() {
    function displayMenus() {
    var currentTab = $("ul.nav-tabs>li.main-item > a.active").attr("href");
    currentTab = currentTab.replace(/#/, "");
    $(".nav-tabs .nav-item.sub-item[main=" + currentTab + "]").stop().animate({
       width: 'show'
    });
    $(".nav-tabs .nav-item.sub-item:not([main=" + currentTab + "])").stop().animate({
       width: 'hide'
    });

    }

    displayMenus();

    $(".nav-tabs > .nav-item.main-item > .nav-link").click(function(event) {

      event.preventDefault();
      $(".nav-tabs > .nav-item.main-item > .nav-link").removeClass('active');
      $(this).addClass('active');

      displayMenus();

    });

});

Check out the latest version on JSFiddle

Answer №2

Give this a shot

.nav-tabs .nav-item.sub-item {
    background: rgba(240,240,240,1);
    margin-left: -5px;
     overflow: hidden;
    white-space: nowrap;
}

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

Combining table rows using the <td> class in JavaScript

I am currently working on refining the JS code to merge rows with common values and classes in a table. The existing code successfully merged common rows but failed to consider their classes. Snippet of the Table code: <tbody id="property_dtl_table_bo ...

How can I create a custom Sweet Alert button in JavaScript that redirects to a specific webpage when clicked?

Here's what I have in my code: swal({ title: 'Successfully Registered!', text: "Would you like to proceed to the Log In page?", type: 'success', showCancelButton: true, confirmButtonColor: '#308 ...

"By clicking on the image, you can trigger the transfer of data to the view.py file

Check out my image in two.html: https://i.sstatic.net/FhKlz.png <td> <img src="http://127.0.0.1:8000/imageAll/101.png" width="100"; height="300"> </td> I need to pass the data value of 101 to my view.py wh ...

Issues with Executing Ajax from an HTML List

Trying to implement Ajax on a web application to display associated data with HTML List from a MySQL database using jQuery and PHP. The database consists of two tables named Item and Items as described below: The PHP files are named index.php and result.p ...

Create a responsive design for a webpage that adjusts font size based on the dynamic type settings in iOS

Is there a way to ensure that my website's font size dynamically adjusts for iOS users? For example, if a user changes the font size in their iPhone settings (Settings > General > Accessibility > Larger Text), I want the webpage font size to ...

Dynamic card resizing to fit changes in window size

Hey, I'm currently working on a card layout and have hit a roadblock. I'd like the images to shrink as the window size decreases proportionally. Goal: Images should decrease in size until 600px width while staying centered up to that point. I& ...

An easy way to enable mobility for BootstrapDialog on mobile devices

Currently, I am utilizing the library available at https://github.com/nakupanda/bootstrap3-dialog in order to create a dialog box. However, my issue arises when viewing the dialog on mobile devices where it exceeds the screen size. On desktops, adjusting t ...

Having trouble loading script files with JSDOM in NodeJS?

I'm currently experimenting with loading an HTML page in jsdom to generate graphs, but I'm facing challenges in getting the JavaScript to execute. Here's the HTML page I'm trying to load, which simply renders a basic graph: <html&g ...

Deleting an element from an array stored in local storage with the help of jQuery

Summary: Developing a front-end wish list feature Tech Stack: Utilizing HTML5 (localStorage), CSS, and jQuery Key Features: Ability to add and delete items dynamically with real-time count display Challenge: Issue encountered when trying to remove added ...

Troubleshooting issue with ellipsis functionality in Bootstrap using flex layout

I have been struggling to include ellipsis after displaying 2 lines of icons. I have tried different approaches and modifications, but nothing seems to work. Seeking assistance from experts to tackle this issue, your feedback is highly appreciated. .elli ...

Having trouble locating the element associated with the 'Accept All' button

Recently, I delved into the world of Selenium and Python webscraping. My current challenge involves locating and clicking the elusive 'Accept All' button on a pesky pop-up (image provided below) that appears when accessing the site in Chrome. Th ...

css element remains stationary and in view

Issue at hand: I have created a logo file named .art-obj1631017189 and I am looking to fix its position. It appears to work fine, but when scrolling down and it encounters the content section, my object falls behind the content (item-layout). I desire to ...

What are two ways to tell them apart?

<?php if($_SERVER["REQUEST_METHOD"] == "POST") { } ?> <form action = "" method = "post"> <label>Name of vegetable</label><br> <input type = "text" name = "vegetable" class = "box" placeholder="Enter Ve ...

Ensuring scope safety in jQuery AJAX requests

My intuition suggests that if I am on a laggy server and the user triggers two events quickly enough, the value of c in the success function will be based on the most recent event, potentially causing func1 to use the incorrect value. This is merely a hypo ...

Execute the function at the top of every hour

I need to trigger a jQuery function on the dot of every hour, like at 11:00, 12:00, 13:00 and so on. Below is the function I want to call: function flipIt() { $('#flip').addClass('animate pulse'); $('#flip').removeCl ...

Why isn't the JQUERY AJAX request showing up on the PHP page?

I have a total of 3 files - CallTasks.JS, opentask.php, and calltask.php. My goal is to make an AJAX call in CallTasks.JS to reach calltask.php, passing a string value to it and displaying the result on opentask.php. I am utilizing a JQUERY selector to ins ...

The data-src tags are functioning properly in the index.html file, but they are not working correctly in angular

I'm still learning about Angular and JavaScript, so please bear with me if my questions seem silly. I've been trying to add a theme to my Angular project. When I include the entire code in index.html, everything works fine. However, when I move ...

How can I effectively incorporate a new option into a dropdown menu created using the msDropdown plugin?

I am currently utilizing the jQuery plugin known as msDropdown to showcase dropdown lists that include images. I have been attempting to dynamically insert a new option and then update a msDropdown dropdown list, with the following code: dropdown.add(jso ...

exchanging the positions of two animated flipdivs

Hey there! I'm facing a little challenge with my two divs (let's call them div1 and div2). One of them has a flip animation on hover, while the other flips on toggle click. My goal is to swap these two divs by dragging and dropping them. I' ...

Angular-Google-Maps - display issues arise when adjusting map size on mobile devices

After implementing my map, I noticed that on mobile or resolutions lower than 768px, the map renders incorrectly. It seems like all the map tiles load in the top left corner and then render correctly. Even setting the resize event did not resolve this iss ...