Animating two elements simultaneously using AngularJS CSS animations

Currently delving into AngularJs, particularly focusing on angular-animate. Within the html, I have a toggle that smoothly transitions the div with the class "my-first-animation". How can I add a transition to the background simultaneously? I want the background color of the body to smoothly fade from white to black when the div with the class "my-first-animation" appears. Any insights on how to achieve this would be greatly appreciated. UPDATE: K.Torres' solution worked like a charm. Thank you. However, despite adding the dark-bg transitions to the CSS as shown below, it doesn't seem to work. Any ideas why? Thanks.

 <!DOCTYPE html>
 <html ng-app="app">
    <head>
        <title> Applying Animations</title>
        <script src="js/angular.js"></script>
        <script src="js/angular-animate.min.js"></script>
        <script type="text/javascript">
            var app = angular.module('app', ['ngAnimate']);
            app.controller('homeCtrl', function ($scope){
                $scope.on = false;
            });
        </script>

        <style>
            /*start*/
            .my-first-animation.ng-enter{
                transition: .5s all;
                opacity: 0;
            }
                .my-first-animation.ng-enter-active{
                opacity: 1;
            }
            .my-first-animation.ng-leave{
                transition: .5s all;
                opacity: 1;
            }
                .my-first-animation.ng-leave-active{
                opacity: 0;
            }



     .dark-bg.ng-add {
        transition: .5s all;
       background:white;

     }
     .dark-bg.ng-add-active {
       background:gray;

     }
    .dark-bg.ng-remove {
       transition: .5s all;
       background:gray;

     }
     .dark-bg.ng-remove-active {
       background:white;

     }
        </style>
    </head>
    <body ng-controller="homeCtrl" ng-class="{ 'dark-bg': on }">
     <button ng-click="on=!on">Toggle Content</button>
     <div class="my-first-animation" ng-if="on">
            This content will fade in over a half second. 
     </div>
    </body>
    </html>

Answer №1

Implement the ng-class directive on the <body> element, with detailed information available in the ngClass documentation.

<body ng-controller="MainCtrl" ng-class="{ 'dark-bg': on }" >

This code snippet will apply the CSS class dark-bg to the <body> tag when the variable on is true.

By clicking on the button, the value of the on variable toggles between true and false. The application of the ng-class directive is conditional on the state of the on variable - when on is true, the dark-bg class is added to the body; when on is false, the dark-bg class is removed from the body.

The subsequent addition of the following CSS styles:

.dark-bg {
   background:gray;
   transition: .5s all;
 }

ensures that the dark-bg class modifies the background color to gray with a smooth transition effect.

To see this functionality in action, check out the Plunker Demo.

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

ngModel unable to bind integer data

Currently, I am encountering an unusual issue with angularjs. I developed an API using Laravel and MySQL that returns data in a json format. On my Ubuntu operating system, the json response from Laravel includes id's and foreign id's as STRING, ...

Tips for locating the specific website address on a publicly accessible webpage

I am looking to receive notifications whenever a company's public website adds a document to their site. This needs to be done for approximately 400 different public sites. Since each site will have a unique document directory, I plan to create a data ...

Experiencing issues with creating HTML using JavaScript?

I'm a JavaScript novice and struggling to figure out what's wrong with my code. Here is the snippet: var postCount = 0; function generatePost(title, time, text) { var div = document.createElement("div"); div.className = "content"; d ...

Guide to creating a d3.js line graph with the help of AngularJS directives

Is there a way to utilize angular directives in order to load the d3.js graph by using scope JSON data instead of relying on a graph.html file? I came across this URLurl. However, I am facing difficulties when trying to implement it for a line chart. If a ...

Rest service encountering issue with @RequestBody functionality

I'm currently working on a web application using AngularJS and WildFly with Spring. However, I've run into an issue with the @requestBody annotation. Here is the service code: @ResponseBody @RequestMapping(value = "/keyuser", method = RequestMe ...

What is the best way to create a menu that can be hovered over but not clicked on

This HTML code includes a dropdown menu created using Bootstrap. It contains navigation links for Home, Add User, Export, and Logout. <ul class="nav navbar-nav navbar-right"> <li <?php if($tabs=='home' or $tabs=='') ech ...

What is preventing me from accessing the $sceProvider?

Struggling to implement a filter using $sceProvider to decode HTML tags. Here's my current code structure: myApp.filter('decodeHtml', function($sce) { return function(item) { return $sce.trustAsHtml(item); }; However, upon integrating ...

Eliminate any horizontal gaps in <ul> by setting list-style-type to none

Check out this informative resource on the various list-style-type values at http://www.w3schools.com/cssref/playit.asp?filename=playcss_ol_list-style-type&preval=none. While the value none still leaves some horizontal space for an empty list symbol, ...

Angularjs - Provider not recognized:

I'm encountering an issue that keeps popping up: Error: [$injector:unpr] Unknown provider: UsersServiceProvider <- UsersService I attempted to address this problem by adding ['UsersService' before my controller function, after reading a ...

code not in a circle uncentered html css

I'm having trouble with centering text inside a circular shape. Getting the alignment right in the middle of the circle is proving to be difficult for me. Here's the code I've experimented with so far: CSS: .circle { margin:auto; border-r ...

Utilizing HTML to call a function and fetching data from AngularJS

I've been struggling to retrieve the value after calling a function in my HTML file. Despite trying various methods after conducting research, I have not achieved success yet. Take a look at the code below: HTML: <div class="form-group"> & ...

Printing in Angular when a value changes is simple and can be achieved by

Looking for a solution to dynamically switch between different departments and list employees within each department as the value changes in Angular. Essentially, I need to display the employees under each department, print the department name when it chan ...

Organizing Content with Bootstrap - Utilizing Sections and Divs

Currently utilizing a Bootstrap template for my website development, I am encountering two challenges that have me stumped: 1) Upon placing my HTML code within a <section>, an automatic top margin/padding seems to be applied to the section. Despite ...

Issues arise when trying to alter the attributes of the :hover pseudo-class

I'm having trouble changing the background-color of list items to black when hovering over them. However, changing the color itself is working fine. .nav li { display: inline-block; height: 100%; padding: 0px 35px; border-width: 0 1px 1px 0 ...

Is it possible in AngularJS to automatically bind an onclick function from the dynamically generated object when utilizing ng-repeat?

I am facing an issue with binding a function from an array of items to an element using ng-repeat in AngularJS. One of the attributes on each item is a custom function, like this: item.myFunction = function(){ // do something } I tried various methods to ...

Could you provide me with some information regarding the relationship between screen resolution and screen size?

When checking my website on different screen resolutions, I rely on a tool called Screenfly from quirktools.com. While using this tool, I came across an interesting feature - the display icon in the top left corner with a dropdown menu showing resolution ...

The comparison between StrictNullChecks and Union Types in terms of syntax usage

Understanding StrictNullChecks in TypeScript Traditionally, null and undefined have been valid first class type citizens in JavaScript. TypeScript formerly did not enforce this, meaning you couldn't specify a variable to potentially be null or unde ...

Creating adaptable web pages with Bootstrap to ensure optimal display across

Create a customized webpage featuring Bootstrap's responsive navigation bar, a circle-shaped image that adjusts to different screen sizes, and a responsive footer. View the layout of the webpage in the following snapshots: Image 1: Web page displaye ...

What could be the reason for the failure of a wrapper that is solely based on

I'm struggling to grasp why using percentages isn't functioning in this scenario. Can someone shed some light on this for me? #wrapper{ position:relative; width:90%; height:90%; background-color: black; } HTML: <body> ...

Leveraging the power of JavaScript to reveal concealed div elements

I've got a collection of divs (five in total) with a hidden class applied to them in my CSS stylesheet. Each div also has its own unique ID. My goal is to use JavaScript to reveal these divs one by one. Here's an example of what I'm looking ...