Create a pure CSS animation that slides the navbar in from the left side

I'm currently developing an Angular application and utilizing a Bootstrap 4 navbar without jQuery or any third-party plugins.

HTML:

<nav class="navbar navbar-expand-md bg-theme">
    <div class="container-fluid">
        <!-- Toggler/collapsible Button -->
        <button class="navbar-toggler" type="button" data-toggle="collapse" (click)="toggleNavigation()" data-target="#collapsibleNavbar">
            <i class="fa fa-bars"></i>
        </button>
        <div class="collapse navbar-collapse" id="collapsibleNavbar">
            <ul class="navbar-nav text-light">
                <li class="nav-item">
                    <a routerLink="/dashboard" href="#">Dashboard</a>
                </li>
                <li class="nav-item">
                    <a routerLink="/template" href="#">Template</a>
                </li>
                <li class="nav-item">
                    <a routerLink="/product" href="#">Product</a>
                </li>
            </ul>
        </div>
    </div>
          <!-- I've duplicated the above navbar-nav for responsiveness. Do we really need this, or can we achieve the same with the existing code? -->
      <div *ngIf="showResponsivenavbar">
            <ul class="navbar-nav text-light bg-custom-light p-3">
                <li class="nav-item pb-2">
                    <a routerLink="/dashboard" href="#">Dashboard</a>
                </li>
                <li class="nav-item pb-2">
                    <a routerLink="/template" href="#">Template</a>
                </li>
                <li class="nav-item pb-2">
                    <a routerLink="/product" href="#">Product</a>
                </li>
            </ul>
      </div>
</nav>

CSS:

.bg-custom-light a {
  color: #fff!important;
}
.bg-custom-light {
    background-color: #e51631;
  }

  .navbar-nav {
    position:fixed;
    top:7%;
    left:0;
    width:150px;
    -moz-transition:all 200ms ease-in;
    -webkit-transition:all 200ms ease-in;
    -o-transition:all 200ms ease-in;
    transition:all 200ms ease-in;
}

I want the navbar to toggle on small screens when clicking the icon bars, showing the navbar below the bars. However, I'd like to add a sliding animation effect where it slides in from the left. Despite adding the necessary CSS transitions, the animation isn't working as expected. Any suggestions on how to achieve this slide-in effect upon clicking the toggle icon bar would be greatly appreciated.

You can view a working demo here

Answer №1

To make a smooth transition, it's important for Transition to have knowledge of both the initial and final states. If an element is removed from the DOM using *ngIf, there won't be anything to animate.

One workaround is to assign a class that indicates whether the element is open or closed:

 [ngClass]="{'open': showResponsivenavbar}"

Then, adjust the CSS so that initially the element is off-screen. When the open class is applied, it will move into view like this:

  .navbar-nav {
    position: fixed;
    top: 7%;
    left: -150px;
    width: 150px;
    -moz-transition: all 200ms ease-in;
    -webkit-transition: all 200ms ease-in;
    -o-transition: all 200ms ease-in;
    transition: all 200ms ease-in;
  }

  .open .navbar-nav {
    left: 0;
  }

With this setup, adding the open class will smoothly transition the menu from left: -150px to left: 0

A modified Stackblitz demonstrating this approach can be viewed here

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

Alignment of the button in a vertical position

Hey there! I've got a button set up with Bootstrap for centering, but now I'm looking to vertically align it in the middle of the page without any margin or padding. Any ideas on how to go about doing that? Here's the HTML code: <div cl ...

Endless cycle of invoking functions in Angular.js

Issue with the template : <div class="container"> <h1>Process Definitions</h1> <table class="table table-stripped" > <tr> <td><strong>Name</strong></td> <td><strong>Id</st ...

The length of Bootstrap Form Radio Buttons varies

Struggling with implementing Bootstrap 4 for a quiz creation project. Attempting to standardize the length of options (Panthers, Fighting Irish, Bears, Tigers). Tried using col-sm-8 classes on all elements without success. Also attempted targeting classe ...

jquery, slideToggle not behaving correctly on mouse hover

Whenever the mouse hovers over my div with the class .frame, I slideToggle a div with the class .content. And when the mouse leaves .frame, I toggle .content back again. Everything seems to be working fine except for two issues: the effect gets messed up i ...

The content of btn-id element in Angular is showing as undefined

I have a JavaScript file located at sample/scripts/sample.js and there are 8 HTML files in the directory sample/src/templates/. My goal is to select a button on one of the HTML files. When I tried using angular.elemnt(btn-id).html(), I received an 'un ...

I am encountering an issue with my Django registration form not functioning properly

Seeking assistance as I am encountering an issue with my form. To elaborate, when I input information in the fields and trigger the submit button, nothing happens- no error messages displayed either. Code from views.py: import django.shortcuts from djang ...

Checkbox: Customize the appearance of the root element

I am trying to modify the root styles of a Checkbox component, but it is not working as expected. Here is my code: <CheckboxItem onChange={()} checked={isChecked} label="Show Checkb ...

Validate the presence of an element on a page using selenium

I attempted to use the following code: if driver.find_element_by_xpath("/html/body/div[2]/div[3]/div[1]/div[1]/button"): pass believing it would be successful, however it did not work and instead resulted in: selenium.common. ...

Utilize jQuery to showcase elements in a dropdown menu

Hey everyone, I'm working on an ASP.NET MVC4 project and I'm using a jQuery script on the edit page. However, I am encountering an issue with displaying elements on the page. Here is the initial HTML markup of my dropdown before any changes: & ...

Avoiding conflicts between banners, text, and images for HTML/CSS design

I'm having an issue with the banner I created for my project. It seems to be overlapping with text and images, hiding behind them. How can I fix this? Unfortunately, I can't post the link to my project here due to other files present. The specif ...

Embedding a styled list item within a list

How can I customize the style of list items within a list? HTML code <span class=bbb> <ul> <li>Preparing archive inquiries about study periods or work experience at LLU: <ul> <li>Within seven days - 5 ...

Encountering an error while trying to generate volume in a custom Docker image on Google Cloud

I am working on an Angular application with Cypress and attempting to set up a pipeline configuration on Google Cloud Build. images: - 'gcr.io/$PROJECT_ID/e2e-tests' steps: # Install node_modules - name: node:10.16.3 entrypoint: npm ...

Dealing with Overflow Issues in Divs

Currently facing an overflow dilemma. My layout is designed to have a white container with rounded corners, while the footer inside the container is a shade of grey. To ensure the rounded corners of the footer div align with the container, I utilized &apo ...

Freezing Columns and Rows for a Spacious Scrollable Table

After extensive effort, I have been diligently striving to create a solution that allows a table's column headers to scroll with the body horizontally and the first column to scroll with the rows vertically. While I've come across solutions that ...

Creating responsive layouts with flexible image sizes using Flexbox

My exploration of Flexbox began by testing it on an existing template sourced from a previous stackoverflow question. I am currently focusing on adjusting the image size to better fit within its parent div. max-width: 50px; height: auto; margin:auto; ...

Tips for retrieving a child component's content children in Angular 2

Having an issue with Angular 2. The Main component displays the menu, and it has a child component called Tabs. This Tabs component dynamically adds Tab components when menu items are clicked in the Main component. Using @ContentChildren in the Tabs comp ...

the process of extracting data from a request body in Angular 2

After creating a URL for end-users to access, I wanted to retrieve data from the request body when they hit the URL from another module. The process involves fetching the data from the request body, passing it to my service, and then validating the respons ...

Implementing SAML Authentication in Angular and .NET WebAPI

I am in the process of setting up a website that utilizes Angular for the user interface, .NET for the backend APIs, and integrates SAML for authentication with a third-party Azure AD. I'm finding it challenging to grasp how each component interacts w ...

Having trouble with the npm install command for setting up the Angular dual list box feature

I attempted to run the npm i angular-dual-listbox --save command, but encountered numerous errors indicating version discrepancies despite having version 8.2.14. I referred to this article: npmjs. The error log is as shown below: C:\GUNEET KAUR\P ...

Javascript variable unable to retrieve value from textbox

Hey there! I am having some trouble reading a textbox that is populated from the server into a JavaScript variable. When I try to access it, I get a console error saying "can't read from NULL". However, the text box is definitely populated with the st ...