AngularJS - Implementing a sliding down menu feature with the ng-click directive

Is there a way to modify my menu button so that it slides down and up when clicked, instead of just showing and hiding?

HTML:

  <div class="nav">
<nav class="primary-nav" ng-show="showMenu" ng-class="{true: 'showMenu'}[showMenu]">
  <ul>
    <li><a href="someurl">Desktop Site</a></li>
    <li><a href="someurl">Link 1</a></li>
    <li><a href="someurl">Link 2</a></li>
    <li><a href="someurl">Link 3</a></li>
  </ul>
</nav>
<div class="nav-slide"> <a href="" ng-click="showMenu = !showMenu">Menu</a> 
</div>

CSS:

.nav {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 10;
}

.primary-nav {
    width: 100%;
    background: #fff;
}
.primary-nav a { display: block; padding: 15px 8px; border-bottom: 1px solid #D1D1D1; color: #666666; font-weight: 700; }
.primary-nav li:last-child a { border-bottom: none; }
.nav-slide { 
    position: relative; 
    top: 0; 
    border-top: 4px solid #fff; 
    -webkit-box-shadow: 0px 5px 4px 0px #000;
    box-shadow: 0px 5px 4px 0px #000;
    z-index: 10;
}
.nav-slide a { 
    display: block; 
    width: 80px; 
    position: relative; 
    right: 10px;
    padding: 2px 0 8px 0; 
    float: right;
    text-align: center;
    color: #333;
    font-weight: 700;
    border-radius: 0 0 6px 6px;
    -webkit-box-shadow: 0px 5px 4px 0px #000;
    box-shadow: 0px 5px 4px 0px #000;
}
.nav-slide a.active { background-position: center -24px }

.showMenu {
    transform: rotate(45deg);
    -webkit-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
}

Check out the plunker for an interactive demo: http://plnkr.co/edit/NvWW0x2d8NPNJAIfDN5F

Answer №1

To make CSS animations work smoothly, it's important to avoid using ng-show/ng-hide on the element you want to animate and instead utilize CSS classes for better control. Below is an example of how you can achieve this, though it might be complex depending on your specific needs:

HTML

<nav class="primary-nav" ng-class="{true: 'showMenu'}[showMenu]">

CSS

.primary-nav {
    width: 100%;
    background: #fff;
    height: 0;
    transition: all 0.5s linear;
}
.primary-nav.showMenu {
  height: 150px;
}
.primary-nav li { display: none; }
.primary-nav.showMenu li { display: initial; }

Check out the live demo here: http://plnkr.co/edit/TK5hX3MXPHBnIwVDNifK?p=preview


You can also explore animating margins instead of height in this example: http://plnkr.co/edit/VMdUALUbhqIpOPfNGDyU?p=preview

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

The concept of undefined functions and the use of dependency injection may not always align

Recently starting with AngularJs, I am honing my skills by developing a single page Todo Application. However, I have encountered an issue while trying to load a localStorage factory that I intend to use for this project. Currently, I am stuck on the error ...

Tips for enabling the wrap property with autogeneratecolumn set to true

In my grid view, I want to ensure that any text in a column that exceeds the width of the column will either wrap within the column or display on a new line. Below is the code for the grid view: <asp:GridView ID="GridView1" AllowSorting="True" runa ...

Discover the simplicity of incorporating pagination into an HTML table with Angular Material

My goal is to implement pagination on my webpage, displaying 3 rows per page and enabling navigation through pages using Angular Material pagination. In usersComponent.ts, I retrieved data from an API: import { Component, OnInit, ViewChild } from '@an ...

Issue with left-aligned navigation in Bootstrap

Having some trouble getting my logo to align correctly on the top navigation bar. It looks fine in a small view, but when I stretch it out, the logo shifts to the right. I want the logo to stay all the way to the left regardless of screen size. I've ...

Developing a fresh feature in Angular.js for transmitting my localstorage model information to a bus?

As a beginner in Angular Js, I have mastered the basics and am now working on storing user input values to localstorage using a form. Although this part is working fine, I need assistance in creating a new service to communicate with my colleague's . ...

I am attempting to increase the date retrieved from my database by three years

I need assistance with updating dates in my PHP script. I want to add 3 years to the date stored in the database for each record. Specifically, I need to retrieve the date when a certain class was first taken (date_aerial) and then return a new date by add ...

Solving Problems with Image Placement using CSS

As I embark on learning responsive CSS, the concept is still quite new to me. One issue I'm facing is the alignment of the images on the second row in comparison to the top images. I aim to have 4 images per row, with the flexibility for the image siz ...

Monitor the change in FileReader().readyState using AngularJS

Below is the AngularJS code I have written to read a local file. var files = document.getElementById("file"); files.addEventListener("change", handleFile, false); function handleFile(event) { SpinnerService.upload(); // Display loading spinner ...

Using filter to convert a string into an array and then utilizing it in ng-repeat functionality

I have a string in the format of "1200:2,1300:3,1400:2" that I need to display like this: <p>1200</p><p>2</p> <p>1300</p><p>3</p> <p>1400</p><p>2</p> I attempted to use a filter, ...

Styling Issues with Angular Code within App-Root Element

I am encountering an issue with my Angular 7 application during initialization. I have a class named "testing" that simply changes the text color to red. I tried placing the class in the index.html file within a style tag, as well as in the styles.scss fil ...

Need help managing floats with the: after pseudo selector?

Struggling with floats and needing to utilize the :after technique, but it's not cooperating. The floats after the red and blue box need to be cleared. Despite trying everything, the issue persists. Here is the provided code: <!doctype html> &l ...

Develop a responsive component on the right side

I am encountering an issue with creating a responsive div that matches the height of the image on the left side. I want to include text in this div, however, when I do so, the paragraph expands in height along with the div element when I attempt to change ...

Preview not showing CSS changes properly

1) I am encountering an issue with displaying CSS in a form preview tab. Despite setting the CSS, it does not reflect on the fields after clicking the preview button. 2) Are there alternative methods to open the tab in a new window rather than opening it ...

jQuery fails to hide DIVs when jQuery.show() has been utilized in a previous event

I've always considered myself pretty proficient in jQuery, but this particular issue has me stumped. Essentially, I have a click event that should hide one DIV (a placeholder) and show two others (an input section and control buttons section). However ...

What is the best way to shift all elements to the right side except for 'tindog'?

https://i.sstatic.net/hAUps.png <nav class="navbar navbar-expand-lg navbar-dark "> <a class="navbar-brand" href="">Tindog</a> <button class="navbar-toggler" type="button" data-t ...

blurring out of an input field and a division element

I am currently working on creating a dropdown suggestion box that hides when the input field and dropdown box are no longer in focus. My HTML code: <input type="text" id="user_address"> <div id="user_address_sg">SUGGESTION</div> <di ...

Is there a way for me to identify the scrolling height and dynamically adjust the title using jQuery?

I am in the process of creating a list of shops from different countries for my website. I want to implement a feature that detects when the user has scrolled to a specific area so I can update the title at the top accordingly. My idea is to assign classe ...

Angular.js: Utilizing ng-repeat to iterate through items within an object

There are two ng-repeats on the page that share an Object. The items should only be repeated if they match the type data. Sample Object $scope.events = [ { date: "1/16/13", time: "11:30", ...

Preserve the scroll location when new items are included in the array within ng-repeat

Is it possible to have a list of messages displayed using ng-repeat and load older messages via ion refresher when the user scrolls to the top? The goal is for the older messages to be added above the current messages while maintaining the scroll position ...

Transform the HTML content into a JSON format file

I'm currently developing an online marketplace and trying to convert all product information like name, price, and description into json format. I have a sample code featuring a selection of products displayed in rows. <div class='cakes'& ...