ng-show will not trigger animation when initially loaded

I am working with the following HTML code:

<div class="container w-xxxl w-auto-xs " ng-controller="SurveyQuizController">
    <div class="panel box-shadow fade-in-right " style="opacity: 0.9" ng-repeat="question in questions" ng-show="current_question == $index">
        <div class="panel-heading b-b">
            <h1 class="h2 margin-none">{{question.question.question}}?</h1>
        </div>
        <div class="panel-body">
            <div class="form-group">
                <multiple ng-show="question.question.question_type_id == 1"></multiple>
            </div>
        </div>
        <div class="panel-footer text-center">
            <strong class="small">{{$index+1}} out of {{questions.length}} questions</strong>
            <div class="clear">
                <a class="btn btn-default pull-left {{$index == 0 ? 'disabled' : ''}}" ng-click="previousQuestion()">Prev</a>
                <a  class="btn btn-primary pull-right" ng-click="nextQuestion()">Next</a>
            </div>
        </div>
    </div>

</div>

Every panel within this code contains the class fade-in-right, which is defined as follows:

.fade-in-right.ng-enter {
    -webkit-animation: fadeInRight 0.5s;
    animation: fadeInRight 0.5s;
}

.fade-in-right.ng-leave {
    -webkit-animation: fadeOutLeft 0.5s;
    animation: fadeOutLeft 0.5s;
}

Interestingly, when an element is hidden using ng-show, the animation associated with the class does not trigger.

Could someone shed some light on why this might be happening?

Answer №1

To eliminate the jumping effect and display only one element at a time, check out Angular: Applying Animations / Animating ngView with CSS Keyframe Animations

angular.module('app', ['ngAnimate']).controller('controller', function($scope) {
  $scope.questions = [{
    title: 'Question 1'
  }, {
    title: 'Question 2'
  }, {
    title: 'Question 3'
  }, {
    title: 'Question 4'
  }, {
    title: 'Question 5'
  }];
});
.view-container {
  position: relative;
}
.fade-in.ng-enter,
.fade-in.ng-leave {
  background: white;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
}
.fade-in.ng-enter {
  -webkit-animation: 0.5s fade-in;
  -moz-animation: 0.5s fade-in;
  -o-animation: 0.5s fade-in;
  animation: 0.5s fade-in;
  z-index: 100;
}
.fade-in.ng-leave {
  -webkit-animation: 0.5s fade-out;
  -moz-animation: 0.5s fade-out;
  -o-animation: 0.5s fade-out;
  animation: 0.5s fade-out;
  z-index: 99;
}
@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@-moz-keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@-webkit-keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@keyframes fade-out {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}
@-moz-keyframes fade-out {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}
@-webkit-keyframes fade-out {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.23/angular-animate.js"></script>
<div ng-app="app" class="container w-xxxl w-auto-xs " ng-controller="controller">
  <div class="btn-group" role="group" aria-label="...">
    <button type="button" class="btn btn-default" ng-click="current_question = 0">1</button>
    <button type="button" class="btn btn-default" ng-click="current_question = 1">2</button>
    <button type="button" class="btn btn-default" ng-click="current_question = 2">3</button>
    <button type="button" class="btn btn-default" ng-click="current_question = 3">4</button>
    <button type="button" class="btn btn-default" ng-click="current_question = 4">5</button>
  </div>
  <div class="view-container">
    <div class="panel box-shadow fade-in" style="opacity: 0.9" ng-repeat="question in questions" ng-if="current_question == $index">
      <div class="panel-heading b-b">
        <h1 class="h2 margin-none">{{question.title}}?</h1>
      </div>

    </div>

  </div>
</div>

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

"Encountered a malfunction in the dialogue system on the second attempt

Looking for a way to pass an array to a Modal Dialog using a template. My approach, based mostly on AngularJS documentation, is working initially but has issues with subsequent openings of the dialog: angular.module("materialExample").controller("calenda ...

Issue with Angular Material Table: Dragged rows do not drop in the correct position when scrolling

I'm encountering issues with using Angular Material Table along with Drag and Drop CDK and scrolling. While dragging a row and then scrolling, the row does not drop where intended. Additionally, the animation does not follow the scroll correctly. I ...

Creating dynamic buttons with event listeners and parameters in JavaScript using jQuery

I am attempting to dynamically create and append buttons to an HTML page with defined events using the following code: var mdata = JSON.parse(data); mdata.forEach(function(k) { $("#routesPage").append("<button class='accordion' onclick=&a ...

Flaw in Basic Function Logic Using HTML, JavaScript, and CSS

Need some help with the function onBall3Click1 (code is at the bottom). The ball should act like a lightbulb: ON - turn YELLOW, OFF - turn GRAY. I've been trying to figure out the logic behind it for so long and can't seem to find the issue... ...

Encountering the error "TypeError: Router.use() is expecting a middleware function but received undefined" when attempting to import a router from another file

Trying to configure a router for user paths and exporting it for use in index.js is causing an error. The following error message is displayed: C:\Users\yugi\OneDrive\Documents\codes\NodeJs\API_nodejs\node_modules&bs ...

Error message received from Express middleware: "Unable to modify headers after they have been sent."

I have created middleware for my Node Express app to perform the following tasks: Checking all incoming requests to determine if they are from a bot Allowing requests for raw resources to proceed Serving an HTML snapshot if the request is from a bot How ...

Masking Aadhaar numbers

I need to find a way to detect when the back button is pressed on an input field. The methods I have tried, e.key and e.which, are returning as undefined in mobile Chrome. How can I make this work? It's functioning properly on desktop. jQuery(funct ...

Disallow the use of restricted globals in a TypeScript project when utilizing Object.values in ESLint

Encountering an ESLint error related to no restricted globals: const objectParamsAllNumbers = (obj: Object) => !Object.values(obj).find(elem => { return isNaN(elem); }); After attempting to include ES2017.Object in the tsconfig, the error i ...

How can I replace specific text with HTML code in a webpage?

In my application, users have the ability to write comments that may contain HTML code. However, this code is escaped before being displayed: <div class="card-body"> <p class="card-text"> &lt;h1&gt;HOLA&lt;/h1&gt; Cita:#2&am ...

Variable used to invoke a Javascript namespace

I've set up a specific namespace called Cookies with sub-namespaces ShowLabels and ShowOptions. My goal is to be able to invoke this namespace on a page using a jQuery value that will either return ShowLabels or ShowOptions. For example: Cookies.$.t ...

Error message: Unable to locate module using import instead of require

I am currently in the process of transitioning from using require to using import for all modules within my project. However, I have encountered some challenges with older npm modules that only provide instructions for require. For example, when it comes ...

Unable to set up npm for node-resemble on macOS

Issue Encountered: Error installing node-resemble package due to missing README data and configure errors. npm install failed with exit status 1. ...

Bind a tree structure in Knockout by linking parent IDs to the primary key column

I want to organize data returned from a stored procedure into a tree structure using the guidance provided in the Knockout documentation. The data I am working with has the following format: ID | Name | ParentID 1 | Parent 1 ...

Step-by-step guide on populating input fields with JSON data for each row

As a beginner in Programming, I have been searching for an answer to the following problem on this site for days but haven't been able to figure it out yet. To explain briefly, there will be an input form where the user can define the number of rows ...

Preventing form submission when selecting an address from Google Maps autocomplete in AngularJS

I am currently facing an issue with the Google autocomplete address search functionality. Whenever I click or press enter after selecting an address, the form automatically submits. I have tried using the event preventDefault but it doesn't seem to be ...

Angular makes it easy to perform HTTP requests using the `http

Greetings! Here is the JSON data for "names" : [ { "file": "file1.zip", "date": "12-03-2016", }, { "file": "file2.zip", "date": "24-06-2016", }, { "file": "file3.zip", "date": "02-12-2016 ...

Switch videos within the video tag as you scroll

I am trying to implement a video tag within a div element on my website. Here is the current code: <div class="phone" align="center"> <div class="phone-inner"> <video width="100%" height="100%" autoplay="" loop="" muted id="phonevideo ...

Using `getJson` to parse the tree structure

I am currently working with a JSON file that contains order data. The structure of the JSON file is as follows: { "orders": [ {"name": "Peter", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="64140110011624050b0 ...

jQuery: Managing and modifying values for dynamically generated input elements

Hello, I am currently working with the latest version of jQuery and have a table set up where clicking on the "Add" button appends a row of inputs. My goal is to calculate the total price for each particular product based on keyup events (i.e., qty * price ...

Displaying a page using express.Router()

I'm having trouble figuring out how to incorporate EJS rendering into a file utilizing express.Router(). The usual method of router.set('view engine', 'ejs') doesn't seem applicable in this context. Any ideas on how I can succ ...