angularjsanimationng-animateng-show

AngularJS ng-show with fade animation


I'm new to ngAnimate and I'm trying to accomplish something really simple.

Show div1 and hide div2 when a button is clicked (I managed to do that). And display a fade animation when a div leaves/shows (I didn't manage to do that)

And I don't want to show both divs at the same time when it leaves/shows.

I prepared a JSFIDDLE

Here is the code

HTML

    <body ng-controller="AniCtrl as test" >
<div ng-app="app" ng-init="visibleDiv='div1'">

    <md-button ng-click="visibleDiv='div1';test.showBoxOne = !test.showBoxOne">Div 1</md-button>
    <md-button ng-click="visibleDiv='div2';test.showBoxOne = !test.showBoxOne">Div 2</md-button>

  <section>
    <div ng-show="visibleDiv == 'div1'" flex>
     <div class="box" ng-show="test.showBoxOne">
          <p>This is Div 1</p>
     </div>
    </div>
    <div ng-show="visibleDiv == 'div2'" id="div2" flex>
     <div class="box" ng-show="test.showBoxOne">
          <p>This is Div 2</p>
          </div>

    </div>
  </section>
</div>
</body>

JS

   angular.module('app', ['ngMaterial', "ngAnimate"]);

app.controller('AniCtrl', AniCtrl);

function AniCtrl() {
  var self = this;

  self.showBoxOne = false;
  self.showBoxTwo = false;


}

CSS

    .box{
  background-color:black;
  color:white;
  border:1px solid red;
}

.box-one {
  -webkit-transition:all linear 0.5s;
  transition:all linear 0.5s;
}

.box-one.ng-hide {
  opacity:0;
  /* transform: scale(0.8); */
}

Thank you


Solution

  • I think this should be the solution.

    Because we are using ng-show we should include the appropriate CSS. Also you cant have the same condition for both divs, so one of the ng-show will be

    <div class="box fade" ng-show="!test.showBoxOne">
              <p>This is Div 2</p>
              </div>
    
        </div>
    

    JSFiddle Demo

    HTML:

    <div ng-app="sandbox" ng-init="visibleDiv='div1';test.showBoxOne=true;">
      <div layout="row" flex layout-align="center">
        <md-button ng-click="visibleDiv='div1';test.showBoxOne = !test.showBoxOne">Div 1</md-button>
        <md-button ng-click="visibleDiv='div2';test.showBoxOne = !test.showBoxOne">Div 2</md-button>
      </div>
      <section layout="row">
        <div class="" ng-show="visibleDiv == 'div1'" flex>
         <div class="box fade" ng-show="test.showBoxOne">
              <p>This is Div 1</p>
         </div>
        </div>
        <div class="" ng-show="visibleDiv == 'div2'" id="div2" flex>
         <div class="box fade" ng-show="!test.showBoxOne">
              <p>This is Div 2</p>
              </div>
    
        </div>
      </section>
    </div>
    

    CSS:

    .box{
      background-color:black;
      color:white;
      border:1px solid red;
    }
    .fade {
        transition: all linear 1s;
        opacity: 1;
    }
    
    .fade.ng-hide {
        opacity: 0;
    }
    .ng-hide {
        opacity: 0;
        transition: none 0;
    }
    

    I hope this solves your issue!