Revealing elements with AngularJS ng-show directive prior to concealing them

Currently, I am in the process of implementing a slide effect for bootstrap badges to showcase hierarchical data relationships using AngularJS.

My goal is to have a slider-effect that smoothly reveals new sub-categories while concealing previously open sub-categories. However, the current implementation first shows the "slide-in" effect followed by the "slide-out" effect, when ideally it should be the other way around.

For instance, upon selecting a badge for a different category, the existing open sub-categories should close before opening the new sub-categories.

The HTML structure is outlined below:

<div ng-controller="MainController">
  <ul ng-repeat="category in categories">
    <li ng-if="category.category_type=='parent'" ng-show="category.category_show">
      <span class="badge badge-p" ng-click="updateResults(category)">{{category.category_name}}</span>
    </li>
    <li ng-if="category.category_type == 'child'" ng-show="category.category_show" class="badge-slider">
      <span class="badge badge-c">{{category.category_name}}</span>
    </li>
  </ul>
</div>

The corresponding CSS is as follows:

.badge-slider {
    max-height: 100px;
    -webkit-transition: max-height linear 0.2s;
    -moz-transition: max-height linear 0.2s;
    -o-transition: max-height linear 0.2s;
    transition: max-height linear 0.2s;
    overflow:hidden;
}

.badge-slider.ng-hide {
    max-height: 0px;
}

I have created a simplified plnkr example to illustrate the issue: http://plnkr.co/edit/S255yk0N2wAXrfq7Mqd6

UPDATE: With assistance from sbedulin, I managed to resolve the problem and enhance the code to dynamically indent subcategories based on their position within the hierarchy. Check out the updated mockup here: http://plnkr.co/edit/5I1pU0TZo6AjHJTbBuG9

Answer №1

After tweaking your CSS, I successfully achieved the desired effect:

/* My Updated Styles */
.badge-slider {
  max-height: 100px;
  -webkit-transition: max-height linear 1.2s;
  -moz-transition: max-height linear 1.2s;
  -o-transition: max-height linear 1.2s;
  transition: max-height linear 1.2s;
  transition-delay: 0.0s;
  overflow:hidden;
}

.badge-slider.ng-hide {
  -webkit-transition: max-height linear 0.0s;
  -moz-transition: max-height linear 0.0s;
  -o-transition: max-height linear 0.0s;
  transition: max-height linear 0.0s;
  max-height: 0px;
}

I have set the transition lengths to 1.2s in .badge-slider to clearly demonstrate its functionality. The crucial step is adding transition-delay: 0.0s; to .badge-slider and setting transition lengths to 0.0s for .badge-slider.ng-hide. I hope this solution proves helpful!

Answer №2

The issue at hand is that using

<ul ng-repeat="category in categories">
creates multiple nested <ul> elements, which should ideally be placed on individual <li> items.

After restructuring the HTML, it will appear as follows:

<ul>
    <li ng-repeat="category in categories"
        ng-init="isChild = category.category_type == 'child'"
        ng-show="category.category_show"
        class="badge-slider">

      <span ng-click="!isChild && updateResults(category)"
            ng-bind="category.category_name"
            class="badge {{ isChild ? 'badge-c' : 'badge-p' }}">
      </span>

    </li>
</ul>

CSS

.badge-slider {
  -webkit-transition: all 0.2s linear 0.2s;
  -moz-transition:    all 0.2s linear 0.2s;
  -o-transition:      all 0.2s linear 0.2s;
  transition:         all 0.2s linear 0.2s;

  line-height: 30px;
  overflow:hidden;

  max-height: 30px;
}

.badge-slider.ng-hide {
  transition-delay: 0.0s;

  max-height: 0px;
}

You can view the functioning plunk 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

ui-router: Issue with ui-sref causing font color to remain unchanged

I've been trying to customize the font color of a navigation link using ui-router's ui-sref functionality, but I'm facing an issue. While I can change the background color by adding it to the .active class in my CSS, the font color remains u ...

Transforming data structures into arrays using Javascript

Could someone help me with converting the following code snippet? const words = {told: 64, mistake: 11, thought: 16, bad: 17} I need it to be transformed into: const words = [ {text: 'told', value: ...

Tips on efficiently adding and removing elements in an array at specific positions, all the while adjusting the positions accordingly

My challenge involves an array of objects each containing a position property, as well as other properties. It looks something like this: [{position: 1, ...otherProperties}, ...otherObjects] On the frontend, these objects are displayed and sorted based on ...

Tips for automatically closing all other divs when one is opened

I have multiple divs structured like this: <div id="income"> <h5 onclick="toggle_visibility('incometoggle');">INCOME</h5> <div id="incometoggle"> <h6>Income Total</h6> </div> </div> <d ...

Is it possible to line up Ajax request in Javascript?

I am seeking a way to schedule an Ajax request to occur every second. The code I currently have in place functions flawlessly. window.onload = function () { setTimeout(doStuff, 1000); // Wait before continuing } function doStuff() { ...

Tips for optimizing the "framerate" (setInterval delay) in a JavaScript animation loop

When creating a JavaScript animation, it's common practice to use setInterval (or multiple setTimeouts) to create a loop. But what is the optimal delay to set in these setInterval/setTimeout calls? In the jQuery API page for the .animate() function, ...

What is the best way to split text copied from a textarea into <p> paragraphs with an equal number of characters in each?

Check out this JSFiddle version I've found a JSFiddle example that seems perfect for my current project needs. However, I'm wondering how to modify the code to ensure that each paragraph is evenly divided with the same number of characters and a ...

Unlocking the Secret: Effortless Transference of an AngularJS Scope Variable from Directive to Controller

Is there a simpler way to transfer an AngularJS scope variable from a directive to a controller? I've come across some complicated examples, but can't we directly access a controller from a directive and update one of its scope variables? ...

Explaining the implementation of JQuery's onload event and its impact on reducing dependencies

Contained within a table is some data https://i.stack.imgur.com/gQyJQ.png Upon clicking the edit button, I am able to modify the information in that specific row. The Menu Category and Menu are populated through Dependent dropdowns. https://i.stack.imgur ...

What is the best way to retrieve a variable within a nested function?

I'm struggling to access a variable from within a nested function in the following code: $(function() { var key = getRandomKey(dictionary); resetInputRow(dictionary[key]); $("#button").click( function() { var answer = key; ...

Explore the latest update in the AngularJS single page application that introduces a new feature specifically designed for the initial login

Our AngularJS single page application has a new feature that we are ready to launch for customer use. We want to inform the logged in user about this new feature so they can start using it. We are looking for a small animated information symbol with a too ...

Personalized cursor that blinks while utilizing window.history.replaceState

While navigating between sub-pages, I utilize the window.history.replaceState method to replace URLs in my web application. However, I have noticed that my custom cursor briefly blinks (replaced by cursor: default) when the current URL is replaced with a n ...

Border shifts on hover effect

After much trial and error, I finally managed to center two links in a nav bar perfectly on the page. I also added a grey bottom border that spans the entire width of the page, with the hover effect turning it blue under the links. Check out this example: ...

Tips for implementing pagination and table layout using Angular.js technology

Suppose I have an object literal containing more than 15 objects that need to be displayed in a visually appealing layout. What is the most effective way to control when the line breaks or the page ends? Currently, I am using ng-repeat on table rows, resu ...

Learn how to retrieve data prior to rendering with Vue 3 and the composition api

Is there a way to fetch data from an API and populate my store (such as user info) before the entire page and components load? I have been struggling to find a solution. I recently came across the beforeRouteEnter method that can be used with the options ...

Angular form validation is unresponsive

Need help with triggering form validation on my website -URL REMOVED-. I've checked my setup but it's still not working when I submit. Any suggestions? ...

Is there a way to slow down the falling effect on my navigation bar?

As I was working on my personal website, I had a creative idea to add a falling-down animated effect instead of keeping the layout fixed. Utilizing bootstrap for navigation, I encountered some difficulty in controlling the speed of the falling effect. Any ...

Create a duplicate of a React component that utilizes the forwardRef method

Imagine you have a foundational component that utilizes forwardRef in this manner: const BaseMessage = React.forwardRef((props, ref) => ( <div ref={ref}> {props.icon} <h2>{props.title}</h2> <p>{props.message ...

Tips on how to bring in .js that has brought in .json from an html file

English is not my first language, and I struggle with it, but I did my best. I am attempting to include a js file that imports json from an html .js import menus from '../json/menus.json'; (function () { function parseMenu(ul, menu) { fo ...

JavaScript arrays storing multiple video lists in HTML5 and displaying them alternately

Creating a mock i-phone screen within a web browser, I have been testing an application currently in development. Everything runs smoothly until the point where I need to introduce another set of videos. Functionality The user interface directs the user ...