如何使用ngAnimate滑动div及其所有内容?

时间:2015-08-22 00:50:09

标签: javascript angularjs fullscreen slide ng-animate

我所拥有的是一个'指数'包含列表和一些选项的页面,然后是右侧的屏幕外div,其中包含特定于当前选择的内容(缺少' show'页面)。点击按钮后,我想要'索引'屏幕上的一半div会滑出视野,并显示' div的一半滑入视图(所以整个div向左滑动)。

要在jQuery中执行此操作,作为临时解决方法,我编写了以下内容:

slideInContent = function () {
    $('#main')
        .animate({'margin-left':'-=100vw'}, 500, 'linear');

},
backToCover = function () {
    $('#main')
        .animate({'margin-left':'+=100vw'}, 500, 'linear');
},

HTML:

<div id='main'>
  <div id='index'>
    ...
  </div>
  <div id='show'>
    ...
  </div>
</div>

CSS:

#index {
  float: left;
}

#show {
  width: 100vw;
  height: auto;
  margin-left: 50%;
  display: inline-block;
}

#main {
  width: 200vw;
  overflow-y: scroll;
}

我如何使用Angular(我假设它将在某个地方的ngAnimate模块中)。

1 个答案:

答案 0 :(得分:1)

我是这样做的,你需要ngRoutengAnimate。我还使用了BootstrapAnimate.css

我们使用CSS为视图设置动画,.ng-enters视图和.ng-leaves

 <style>
  .main-view.ng-enter,
  .main-view.ng-active {
   animation: slideInRight 1s ease;
   -webkit-animation: slideInRight 1s ease;
   -moz-animation: slideInRight 1s ease;
   -o-animation: slideInRight 1s ease;
  }

  .main-view.ng-leave {
   display: none;
  }
 </style>

HTML

<body ng-app="storeApp" ng-controller="StoreCtrl">

 <section id="store">

  <div class="col-xs-3" id="store-categories">
     <a ng-repeat="category in categories" class="btn btn-default btn-block" ng-href="#{{category}}">{{category}}</a>
  </div><!-- end col-xs-3 -->

  <div class="overflow-wrapper">
   <div class="col-xs-9" id="store-items">
    <div class="main-view" ng-view></div>
   </div><!-- end store-items -->
  </div>

 </section><!-- end store -->

 <!-- Vendor JS -->
 <script src="vendors/angular.min.js"></script>
 <!-- Deps -->
 <script src="vendors/angular-animate.min.js"></script>
 <script src="vendors/angular-route.min.js"></script>

 <!-- App.js -->
 <script>
  angular.module('storeApp', ['ngAnimate', 'ngRoute'])

  .config(['$routeProvider', function($routeProvider) {
   $routeProvider

   .when('/', {
    redirectTo: '/category1'
   })

   .when('/category1', {
    templateUrl: 'js/views/category-1/index.html',
    activetab: 'category1'
   })

   .when('/category2', {
    templateUrl: 'js/views/category-2/index.html',
    activetab: 'category2'
   })

  }])

  .controller('StoreCtrl', ['$scope', function($scope) {

   $scope.categories = ['category1', 'category2'];

  }]);
 </script>

</body>