Having trouble getting the CSS transition effect to work properly

With the click of a button, I alternate the opacity of an element between 0 and 1.

A transition effect is supposed to take place for the opacity property.

However, the transition effect does not seem to work as expected.

Check out this link to see the plnkr in action: http://plnkr.co/edit/hrxtvxIIAysEw1iF2DLn

Here are the initial properties set:

.part1 {
  opacity: 0;
  transition: all 5ms linear;
}

.part2 {
  opacity: 0;
  transition: all 5ms ease-in;
}

.part3 {
  opacity: 0;
  transition: all 5ms ease-in-out;
}

The directive containing the transition property looks like this:

 scope.$watch(attrs.showMe, function(newValue) {

        if (newValue === true) {
          element.css({
            'opacity': 1,
            'display': 'block',
            'background-color': attrs.myColor
          });
        } else {
          element.css({
            'opacity': 0,
            'display': 'none'
          });
        }

      });

Answer №1

The reason for the transition not occurring is due to the simultaneous change in the element's display property from block to none. When the transition property is set to 'all', the browser postpones applying the display change until the end of the transition period. Consider removing the display alterations or switching the transition property exclusively to opacity.

Answer №2

Samantha Smith, Greetings! I noticed a few typos in your code, such as unclosed div tags and missing semicolons. I also made some adjustments to the CSS styles.

Feel free to check out this operational Plunkr to see if it aligns with your desired functionality, particularly regarding the red/blue divisions within the modal.
Referencing the code in your Plunkr, which differs slightly from what you have displayed above.

my-modal.html

<div class="modal-body">

       <div class="part1" ng-show="active === 'part1'">
           <p>I am part 1</p>
       </div>

       <div class="part2" ng-show="active === 'part2'="active === 'part2'" >
           <p>I am part 2</p>
       </div>

</div>

style css

.part1 {
  background-color: blue;
  height: 200px;
    -webkit-animation-name: part1; 
    -webkit-animation-duration: 4s; 
    animation-name: part1;
    animation-duration: 4s;

}

@-webkit-keyframes part1 {
    from {opacity: 0;}
    to {opacity: 1;}
}
@keyframes part1 {
    from {opacity: 0;}
    to {opacity: 1;}
}

.part2 {
  background-color: red;
  height: 200px;
    -webkit-animation-name: part2; 
    -webkit-animation-duration: 4s; 
    animation-name: part2;
    animation-duration: 4s;

}

@-webkit-keyframes part2 {
    from {opacity: 0;}
    to {opacity: 1;}
}
@keyframes part2 {
    from {opacity: 0;}
    to {opacity: 1;}
}

app.js

var app = angular.module('plunker', ['ui.bootstrap']);

app.controller('MainCtrl', function($scope, $modal) {
  $scope.name = 'World';

  $scope.open = function() {

    $modal.open({
      templateUrl: 'my-modal.html',
      controller: 'Controller1'
    });

  }
});

app.controller('Controller1', function($timeout, $scope) {

  $scope.active = 'part2';

      $timeout(function() {
        $scope.active = 'part1';
      }, 3000);

    $timeout(function() {
        $scope.active = 'part1';
      }, 3000);
});

index.html

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <link data-require="bootstrap-css@*" data-server="3.3.1" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfbeb1b8aab3beadf1b5ac9feef1ecf1a7">[email protected]</a>" src="https://code.angularjs.org/1.3.16/angular.js" data-semver="1.3.16"></script>
    <script data-require="ui-bootstrap@*" data-server="0.13.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.min.js"></script>
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b110a0e1e09023b49554a5548">[email protected]</a>" data-server="2.1.3" src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script data-require="bootstrap@*" data-server="3.3.2" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>

    <button ng-click="open()">Open me</button>
  </body>

</html>

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

Having trouble hiding the message "Not found" with ng-hide and filters in AngularJS

I am currently working on incorporating instant search functionality with filters in AngularJS. My goal is to have the "Not Found!!" message displayed when the filter results in an empty array, indicating that no matches were found. However, I have encou ...

CSS implementation of a treeview with a dropdown submenu

I'm currently customizing an HTML tree view using CSS, and my goal is to have sublists smoothly slide down whenever a node is expanded. Take a look at my streamlined index.html : <!DOCTYPE html> <html> <head> <script data ...

What is the best way to align text and images for list items on the same line?

HTML:- ul { list-style-image: url(https://i.sstatic.net/yGSp1.png); } <ul> <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit, consequatur?</li> <li>Lorem ipsum dolor sit amet, consectetur adipisici ...

Dispense CSS using a React element

My React component index.js contains styling in style.css. I am looking to share this component on npm, but I am unsure of how to simplify the process for other developers to use the styling. After exploring different options, I have come across three ap ...

The Facebook comment section is taking control

<div class="fb-comments" data-href="https://www.facebook.com/pages/Societ%C3%A0-Ginnastica-Triestina-Nautica/149520068540338" data-width="270" data-num-posts="3"> I'm having trouble with the comment box extending past my div height and overlapp ...

Tips for aligning a select and select box when the position of the select has been modified

Recently, I encountered an interesting issue with the default html select element. When you click on the select, its position changes, but the box below it fails to adjust its position accordingly. https://i.stack.imgur.com/SwL3Q.gif Below is a basic cod ...

Changing the CSS class of list items in a navigation: Tips and tricks

Looking to create a navigation menu that changes the CSS class to 'active' when selected? Take a look at the code below: <ul class="smallmenuContainer"> <li><a class="active" href="index.html#red">Home</a></li&g ...

Struggling to access the Angular Route

I am facing an issue where I can't seem to open the route "/d", while "/" is working perfectly fine. I have tried various troubleshooting methods but unfortunately, haven't been able to find a solution yet. var myApp = angular.module('myApp ...

Achieving uniform cell height distribution in Foundation XY-grid using flexbox

When utilizing the Foundation XY-grid, I encountered an issue with distributing cell heights evenly within a nested grid-y inside a cell using the .auto class on the parent cell. In a scenario with 2 cells in a grid-y, the desired outcome is 50%/50%. This ...

The transparent sticky navigation bar isn't functioning as intended

I stumbled upon a code for the sticky navbar that I found on this website. Intrigued, I decided to copy the code and test it out on my Laravel blade file. The output turned out like this: https://i.sstatic.net/lexRl.jpg Here is how I copied the code: CS ...

I would like a div element to slide up from the bottom of the page

Could someone please assist me in creating a popup div that appears from bottom to top when a specific button is clicked? I would like the div to be fixed without affecting the overall page content. Any help would be greatly appreciated. Thank you! $( ...

AngularJS - The $cancelRequest method is not accessible within the $resource module

I have been attempting to trigger an abort request but I am not receiving the $cancelRequest object from the result of $resource. However, I am able to access the $promise and $resolved objects. Why is this occurring? How can I retrieve the $cancelRequest ...

Notifying the Player of Victory in a Javascript Tic Tac Toe Game: The Winning Alert

Recently, I decided to dive into Javascript and kick off my very first game project - a Tic Tac Toe game. If you're curious to see the code for my project, you can check it out here: Tic Tac Toe Project One feature I'm eager to implement is a n ...

Unleashing the power of specific dates with the angularJS datepicker directive

I am looking to develop a custom Datepicker directive using Angular JS, with the requirement of allowing only specific dates for selection by the user. For instance, I have a predefined list of numbers such as 1,3,5,7, and my goal is to make these particu ...

Tips for making an appended element match the height of an image

After embedding the div .soonOverlay into specific .smallCatalogBlock's, I am facing difficulty in adjusting the height of soonOverlay to match only the height of the img within smallCatalogBlock. Currently, its height extends throughout the entire co ...

NodeJS allows for seamless uploading of files

I'm encountering difficulties when trying to upload a file using nodeJS and Angular. I've come across some solutions, but they all involve Ajax which is unfamiliar territory for me. Is there a way to achieve this without using Ajax? Whenever I ...

What is the best way to center my error message in Simple_form for Ruby on Rails?

Currently, my form is displaying an error message (Name is a required field) in this format: However, I want to position the 'can't be blank' error next to the 'Name', like so: To achieve this layout, I made modifications to the ...

Utilizing JQuery to Implement ngModel and ngBind in Angular Directives: A Step-by-Step Guide

[Note] My objective is to develop custom Angular directives that encapsulate all the necessary JS for them to function. The directives should not know what they are displaying or where to store user input values; these details will be passed in as attrib ...

Angular: Merging arrays of objects and consolidating their values

When I make an http.get request, I receive an array of objects structured like this: [{ "id":12345, "resource_state":2, "external_id":"abscdgrft", "upload_id":1234567, "athlete":{ "id":123456, "resource_state":2, ...

Manipulate the Array in a $resource object with AngularJS

After accessing my oracle REST-API, I receive a JSON response containing an array of tuples from a database table. To interact with this data, I am utilizing the $resource API provided by angularJs. The structure of my result is as follows: Resource {$pro ...