What is the best way to create a smooth transition for a bootstrap navbar in chrome, toggling from right to left on

I have successfully modified the bootstrap navbar to toggle from right to left instead of top to bottom using the following code:

For HTML-

<nav class="navbar navbar-inverse" role="navigation" style="height: 55px; padding-top: 2px; background-color: #000;">
            <div class="container-fluid">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#" id="a_logo_s_s">
                        <img src="images/Png - Only Logo - Variant 2.png"  class="img-responsive" id="logo_s_s" >
                    </a>
                    <a class="navbar-brand" href="#" id="a_logo_l_s">
                        <img src="images/Png - Only Text - White.png" class="img-responsive" id="logo_l_s" >
                    </a>
                </div>

                <div class="collapse navbar-collapse" id="myNavbar" style="height: 346px !important">
                    <ul class="nav navbar-nav navbar-left">
                        <li class="active" id="l_hiw"><a href="#s3" id="hiw" class="navLink">HOW IT WORKS</a></li>
                        <li id="l_cp"><a href="#s4_1" id="cp" class="navLink">COACHES</a></li>
                        <li id="l_cp"><a href="#s5" id="au" class="navLink">ABOUT</a></li>
                    </ul>

                    <ul class="nav navbar-nav navbar-right">
                        <li id="l_el"><a href="#" id="el">ENROLL</a></li>
                        <li id="l_ma"><a href="#" id="ma" class="navLink">MANAGE ACCOUNT</a></li>
                        <li id="l_aq"><a href="#cftr" id="aq" class="navLink">CONTACT</a></li>
                    </ul>
                </div>
            </div>


</nav>

CSS-

.is_open{
    width: 240px;
}
#myNavbar{
    position: fixed;
    padding-left: 10px;
    padding-right: 0px;
    right: 0px;
    height: 100%;
}

JS-

$('[data-toggle="collapse"]').on('click',function(e) {
         e.preventDefault();

        setTimeout(function(){
            $("#myNavbar").css({
            "height":"100%",
            });
        },10);

        $("#myNavbar").toggleClass('is_open',1000);

 });

Although the functionality works initially, after multiple clicks it starts to malfunction as described below:

  1. On the first click, nothing happens.
  2. On the second click, the navbar briefly appears and then disappears again, indicating that both clicks are registering simultaneously.

Edit 1: I attempted to address this issue by utilizing various CSS properties such as "right" and "transform", along with jQuery's "animate" function. However, on alternate clicks, the values of 0 and -240px result in a slight jerk during the showing and hiding process. This behavior is specific to Chrome, whereas other browsers display the expected functionality. This erratic behavior persists across all browsers during the first and second clicks.

Edit 2: To mitigate the abrupt slide effect caused by the sudden change in navbar height from 1px to 100%, I introduced the following code snippet:

       setTimeout(function(){
            $("#myNavbar").css({
            "height":"100%",
            });
        },10);

In Chrome specifically, despite setting "height: 100%" in the inline style attribute of the navbar element, the height reverts to 1px after the initial click. To maintain the desired height, the use of "setTimeout" was necessary to refresh the height value after each click event.

This inconsistent behavior could be responsible for the minor jerk observed during the showing and hiding of the navbar, as the height transitions from 1px -> 100% -> 346px. It's worth noting that this issue seems isolated to Chrome.

Answer №1

Utilizing jquery animate can help you achieve a more polished animation effect.

$(function(){
    var counter = 0;
    $(".navbar-toggle").click(function(){
        $("#myNavbar").stop().animate({right:2*100 }, 'slow');
    });  
}); 

Check out this example:

https://codepen.io/anon/pen/MvZqwM

Answer №2

We were able to successfully address the issue at hand, which had a few key flaws:

  1. One major flaw was that a custom CSS file was added before the Bootstrap CSS file. This resulted in the browser overriding the custom CSS with the Bootstrap CSS, regardless of how many times we defined the style of an element in HTML or CSS. By rearranging the order, we were able to declare height only once and eliminate the need for using setTimeout(expandHeight, 10) on every click.

  2. The use of .stop() (the stop function) before .animate (the animation function) is crucial to prevent any previous ongoing animations from interfering with the current one. Without this precaution, the browser may produce glitches during transitions.

  3. Furthermore, in Bootstrap, .collapse was defined with display:none, causing the navbar to become invisible after a click because our custom CSS did not include such declarations.

A special thank you goes out to Ovidiu Unguru for his assistance!

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

A guide to implementing daily function calls for a week utilizing the @nestjs/scheduler module

Is there a way to schedule a function to run every day for a period of 7 days using Nestjs (@nestjs/scheduler)? @Cron(new Date(Date.now() + (24*60*60*1000) * 7) function() { console.log("This should get called each day during the next 7 days") ...

jQuery receiving improperly formatted HTML from method call

One issue I am facing involves a jQuery Ajax method that anticipates receiving HTML as the return. The JavaScript function's URL leads to a JsonResult method within ASP.NET MVC. To construct the HTML, I am utilizing StringBuilder and checking it in Vi ...

"Enhancing Webpages with AngularJS: The Power of Binding and Re

Using AngularJS to load AJAX content and ng-repeat to generate a list of items. I have a placeholder {{noun}} in the content, expecting it to be replaced with data from $scope.noun when the AJAX content is loaded. However, this does not happen automaticall ...

Do [(ngModel)] bindings strictly adhere to being strings?

Imagine a scenario where you have a radiobutton HTML element within an angular application, <div class="radio"> <label> <input type="radio" name="approvedeny" value="true" [(ngModel)]=_approvedOrDenied> Approve < ...

MySQL database not reflecting changes made via ajax code

I have an issue with my ajax code, where it correctly displays errors and success messages on the page but fails to update the database when I click the submit button. The form includes a select dropdown menu that is populated with options from the databa ...

Using JavaScript to automate keystrokes programmatically

Is there a way to programmatically close a webpage using the keyboard shortcut control + W? I'm wondering if I can write code that will mimic this specific shortcut (control+W). ...

Using Node JS to serve an HTML file along with cascading style sheets

As I delve into pure node.js, I encountered an issue related to the HTTP protocol. After spending hours searching and testing my code, I finally managed to serve my HTML page with CSS successfully. Take a look at my code snippet below: const server = http ...

creating a table displaying data in a horizontal format sourced from ajax/json transformation

I am currently working on a piece of code that retrieves a list of IDs from local storage and compares them to IDs in a JSON file. If there is a match, the corresponding data is displayed in a table. However, I am facing an issue with my table layout as i ...

Angular URL containing dynamic path parameters

I am looking to update the Angular URL to /jobs/id. I have written the code below, but I'm unsure if it will work: $location.path("/JobHire/jobs/"+response.data.id); How should I set up the route configuration? Currently, I have it configured like t ...

Guide to invoking an API in Next.js 13 by utilizing specific variables within a client component

I currently have a collection of products that are accessible on my website through a straightforward function within a server component. async function getData() { const res = await fetch(`${apiPath}`); const data = (await res.json()) as PackProps ...

Handling onMouseLeave event for Popover component in material ui library with hiderBackdrop parameter specified

<Popover key={element.id} className={classes.popoverItem} classes={{ paper: classes.paperElement }} open={isOpen} anchorEl={this.myRef.current} anchorOrigin={{ vertical: 'bottom&apo ...

How can the Jquery UI Datepicker value be automatically set upon redirecting to this page?

When I submit a form and an error occurs, the value selected in the datepicker is not retained. The datepicker field remains empty. How is this possible? <html> <head> <script type="text/javascript" >. $(function() { ...

React - Anticipated an assignment or invocation of a function

Recently starting my journey with reactjs and encountered a small issue. A pesky error keeps popping up saying: Expect an assignment or function call. It's related to the User function, however, I do call that function when creating a new object. Any ...

Toggle the status of active to inactive instantaneously with the click of a

Incorporating DataTables with ajax using PHP CodeIgniter Framework has presented me with a challenge. I am struggling to toggle between Active and Inactive buttons seamlessly. My desired outcome: When the Active button is clicked, it should transition ...

Unable to prolong jQuery ajax call beyond 4 minutes, despite setting timeout, resulting in net::ERR_EMPTY_RESPONSE error

I have a node.js server up and running, and I am utilizing ajax calls via jquery to interact with the server. One of these ajax calls is anticipated to last approximately 5 minutes. However, every time I make this call, it abruptly times out at exactly 4 m ...

Having trouble generating an HTML table from JSON data using JavaScript

My attempt to generate a table with data from an external .JSON file using JavaScript is not working as expected. Everything worked fine when the data was hardcoded into the .JS file, but once I tried to fetch it externally using "fetch", the details do no ...

Allow only specific inner divs to dictate the width of the outer div

I'm working with the following HTML structure: <div id="container"> <div id="block1"> <img src="someimage/path" /> </div> <div id="block2"> Some Text </div> <div id="block3"&g ...

Combining existing CSS classes with node labels in Cytoscape JS for Angular: A Guide

My project follows a consistent CSS theme, but the node's CSS style doesn't match. I'm looking to adjust the label colors in the CSS based on whether it's day mode or night mode. How can I accomplish this? this.cy = cytoscape({ con ...

The web method within the aspx page is failing to execute

On page load, I am attempting to make an ajax request using the AngularJS $http service to fetch JSON data from a web method located in my User.aspx.cs page. The web method is defined as follows: [WebMethod] [ScriptMethod(ResponseFormat=ResponseForma ...

Having trouble getting web components registered when testing Lit Element (lit-element) with @web/test-runner and @open-wc/testing-helpers?

Currently, I am working with Lit Element and Typescript for my project. Here are the dependencies for my tests: "@esm-bundle/chai": "^4.3.4-fix.0", "@open-wc/chai-dom-equals": "^0.12.36", "@open-wc/testing-help ...