简体   繁体   中英

How to add fading effect between two pages using Angularjs routing?

I want to add some animation to my angular webpage which uses routing. I am looking for fadein/fadeout effect just like when you click Next in this page: https://app.enhancv.com/ . So that when I click on any of the list item in header, another page floats in with the same effect. How can I do that using angular routing? You can find the required files below.

Index.html

<!DOCTYPE html>

<!-- define angular app -->
<html ng-app="myApp">

<head>
  <!-- SCROLLS -->
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />

  <!-- SPELLS -->
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
  <script src="script.js"></script>
</head>

<!-- define angular controller -->
<body ng-controller="mainController">

  <nav class="navbar navbar-default">
    <div class="container">
      <div class="navbar-header">
        <a class="navbar-brand" href="/">Angular Routing Example</a>
      </div>

      <ul class="nav navbar-nav navbar-right">
        <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
        <li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
        <li><a href="#contact"><i class="fa fa-comment"></i> Contact</a>

</li>
          </ul>
        </div>

  </nav>  

<div id="main">

    <!-- angular templating -->
        <!-- this is where content will be injected -->
    <div ng-view></div>

  </div>

</body>

</html>

Pages are:

About.html

 <div class="jumbotron text-center">
        <h1>About Page</h1>

        <p>{{ message }}</p>
   `</div>

Contact.html

<div class="jumbotron text-center">
    <h1>Contact Page</h1>

    <p>{{ message }}</p>
    </div>

home.html

<div class="jumbotron text-center">
    <h1>Contact Page</h1>

    <p>{{ message }}</p>
    </div>

Script.js

// create the module and name it myApp
    var myApp = angular.module('myApp', ['ngRoute']);

    // configure our routes
    scotchApp.config(function($routeProvider) {
        $routeProvider

            // route for the home page
            .when('/', {
                templateUrl : 'pages/home.html',
                controller  : 'mainController'
            })

            // route for the about page
            .when('/about', {
                templateUrl : 'pages/about.html',
                controller  : 'aboutController'
            })

            // route for the contact page
            .when('/contact', {
                templateUrl : 'pages/contact.html',
                controller  : 'contactController'
            });
    });

    // create the controller and inject Angular's $scope
    myApp.controller('mainController', function($scope) {
        // create a message to display in our view
        $scope.message = 'Everyone come and see how good I look!';
    });

    myApp.controller('aboutController', function($scope) {
        $scope.message = 'Look! I am an about page.';
    });

    myApp.controller('contactController', function($scope) {
        $scope.message = 'Contact us! JK. This is just a demo.';
    });

I wrap my content in a container that is displayed only when the data is loaded:

<div ng-show="yourpage.dataLoaded" class="fade-element">
 ...
</div>

CSS

.fade-element{
  transition: all linear 0.1s;
  opacity: 1;
}
.fade-element.ng-hide {
  opacity: 0;
}

.fade-element.ng-hide can be used in an element that fades out, for example a preloader:

<div ng-hide="yourpage.dataLoaded" class="preloader fade-element"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM