Animating a class without moving the element using ngAnimate

Looking to achieve a smooth animation effect where the current visible div fades out and another div fades in its place upon clicking the pagination button.

I have successfully implemented the fade in and fade out transitions for the divs.

However, during the transition, the div that is about to fade in gets displaced below the fading out div.

This is my current setup:

// html
<div ng-show="isGroupActive(1)" class="group"></div>
<div ng-show="isGroupActive(2)" class="group"></div>
<div ng-show="isGroupActive(3)" class="group"></div>

<button ng-click="activateGroup(1)">1</button>
<button ng-click="activateGroup(2)">2</button>
<button ng-click="activateGroup(3)">3</button>

// css
.group.ng-hide-add-active {
  display: block!important;
  -webkit-transition: 0.5s linear all;
  transition: 0.5s linear all;
}
.group.ng-hide-remove-active {
  display: block!important;
  -webkit-transition: 0.5s linear all;
  transition: 0.5s linear all;
  -webkit-transition-delay: 0.5s;
  transition-delay: 0.5s;
}
.group.ng-hide {
  opacity: 0;
}

.group.ng-show-add-active {
  display: block!important;
  -webkit-transition: 0.5s linear all;
  transition: 0.5s linear all;
}

.group.ng-show-remove-active {
  display: block!important;
  -webkit-transition: 0.5s linear all;
  transition: 0.5s linear all;
  -webkit-transition-delay: 0.5s;
  transition-delay: 0.5s;
}
.group.ng-show {
  opacity: 1;
}

// js

function MainController($scope) {

  var groupActive = 1;

  $scope.isGroupActive = function(page) {
      return page === groupActive;
  };

  $scope.activateGroup = function(page) {
      groupActive = page;
  }

}

var app = angular.module('app', ['ngAnimate']);

app.controller('MainController', MainController);

In order to prevent the displacement of the div, is it possible to achieve this by adjusting the CSS transition classes?

Answer №1

Simply changing the css classes won't do the trick, but with some creative use of CSS and a bit of additional markup, you can achieve the desired effect (referred to as "cross-fade"). Here's how:

Start by adding a containing element to hold the elements so they can be positioned absolutely:

<div id="container">
    <div ng-show="isGroupActive(1)" class="group"></div>
    <div ng-show="isGroupActive(2)" class="group"></div>
    <div ng-show="isGroupActive(3)" class="group"></div>
</div>

Give the container a style of position: relative:

#container {
    position: relative;
}

Then set the divs that need to cross-fade to have position: absolute:

#container > div {
    position: absolute;
    top: 0;   // adjust accordingly
    left: 0;  // adjust accordingly
}

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

Reveal concealed content when hovering over it

Is there a way to make a hidden div with a button visible when hovering over the 'person-wrap' div? Should I rely on CSS tricks or utilize JQUERY for this task? You can check out the JSFIDDLE example here: http://jsfiddle.net/ceTdA/3/ The desir ...

Ways to incorporate several modals into your interface

There are 2 modals on my website, but when I click on the second modal labeled "Mouthoff," it displays the contents of the Subscribe modal instead. I want each modal to display different content. I have searched on various platforms including here and You ...

Issues with JQuery draggable functionality not functioning as expected

Is there a way to drag bookmarks on a line and have the text above them change to show the percentage from the right edge? Here is a quick snippet that achieves this, along with a demonstration in this fiddle. Check out the code below: HTML <div id=& ...

Tips on creating vertical stacked content utilizing CSS

Has anyone come across this game before? https://i.sstatic.net/hFX9o.png I am trying to stack each card in a column, here is my fiddle jsfiddle. In my scenario, I have cards wrapped in div elements with a maximum height. If a card exceeds this height, i ...

Guide to showing the current time with ng-forms

Implement a time input using ng-forms <input type="time" class="form-control" path="time" formControlName="time" [ngClass]="{'is-invalid': submitted && f.time.errors}" /> ...

Center an absolutely positioned element horizontally within a relative positioned container

Struggling to center an inner element with a position: absolute property inside of a position: relative element? Take a look at this code snippet (https://jsfiddle.net/qL0c8cau/): html,body,div {margin:0;padding:0;} .one { position: relative; margin ...

"Enhance your Vue.js application with the powerful capabilities of vue3-easy

I am currently working on a Vue.js project utilizing the "vue3-easy-data-table" library and following the style recommendations outlined on this particular webpage: Despite attempting to apply the following CSS properties: --easy-table-body-even-row-font ...

The impressive wipe effect in velocity.js using css and jquery

I am looking to achieve a slide effect similar to Powerpoint's wipe using css, jquery, or velocity.js. Here is a video showing the desired effect: https://www.youtube.com/watch?v=VSBB0wfccws I attempted to use css3 masking (svg/gradient) but was uns ...

Adjust the transparency of an image preview using jQuery

Currently, I am working on creating a video gallery for a friend and I am experimenting with changing the thumbnail opacity using jQuery fade effect. While I can achieve this with CSS, I want to implement it with jQuery for a smoother transition. If you ...

What is the best way to iterate through values within my JSON document?

Let's say I have a JSON file and I want to iterate through the values in this manner: var myModel = {"id": 0, "date": "2014-10-28", "amount": 1111, "productId": "2", "description": "Cash"}; for (value in myModel) { //element(by.model(key) ...

What steps can be taken to implement CSS rules on an HTML element with ExtJS?

In my ExtJS 4 project, I am working on drawing an image inside a panel and slider field. My goal is to adjust the opacity of the image based on the value of the slider field. I understand that I can use CSS rules like opacity and filter:alpha(opacity=); t ...

Exploring a List of Divisions Based on User Input

My input field has a special functionality - Pressing the down arrow key will shift focus from the input to the first element in the list of divs below it, moving downwards as you continue pressing the down key. Pressing enter will set the text in that div ...

Unusual glitch found in Angular application's JavaScript code

I am completely puzzled by this issue: I have a Angular app and I'm trying to update the class using a simple JavaScript string update where the class name is bound to the DOM. The strange thing is that in my application, the most basic statement for ...

The Mobile Side Menu Is Not Scrollable

I've encountered an issue with the side menu on my website. While it works perfectly fine on PC, it refuses to scroll on mobile devices. I've tested it on both my iPhone 5 and iPad, but no luck. Additionally, the toggle button isn't function ...

What could be the reason my Bootstrap 5 dropdown menu is not functioning correctly?

As of late, I've delved into web development and have been utilizing Bootstrap v5.0 for creating a website. The code below is from a file named "grage.php": <!-- HERE SHOULD BE THE CODE OF GARAGE --> <div class="container-fluid" ...

`Why isn't Angular updating when watching window.pageYOffset?`

Within my controller, I have the following code: $scope.woffset = window.pageYOffset; $scope.$watch("woffset", function (newValue, oldValue) { console.log("hello"); console.log(window.pageYOffset); }, true); }); My expectation is to receive co ...

AngularJS scope watch isn't firing as expected

In the scope, I store a filtering string for dates. $scope.myModel = {date:""}; Utilizing a jQuery datepicker, <input date-value="myModel.date" date-picker /> This directive updates the string through AngularJS - Attribute directive input value c ...

The concept of nesting templates in Angular

I'm encountering an issue with my directive that has an item template recursively referencing itself, but it's not rendering out children beyond the first level. Despite checking various examples, the only difference I can spot is that my items a ...

The issue of data not being displayed on the page from the controller is persist

Can someone help me figure out what I'm doing wrong here? Here's my HTML code: <div data-ng-controller="documentPreviewCtrl"> <h1> Header {{testHeader}} </h1> <div id="bodyContent"> {{tes ...

Ensuring each div element has a unique bullet icon: A step-by-step guide

I am looking to dynamically create bullet icons based on the number of div elements present in a slider. For instance, if there are 2 slider divs, I want to generate 2 bullet icons within another div. If there are no div elements, then the bullets should ...