ngView页面切换动画(ng-view)

这篇博客探讨了如何在AngularJS应用中使用ngView实现页面切换时的动画效果。通过实例展示了在page-home.html、page-about.html和page-contact.html之间切换时,如何添加动画以提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="bootstrap.min.css">
        <script src="jquery.min.js"></script>
        <script src="angular.js"></script>
        <script src="angular-route.min.js"></script>
        <script src="angular-animate.js"></script>
        <script src="bootstrap.min.js"></script>
        <script type="text/javascript">
            var app = angular.module('myapp', ['ngRoute', 'ngAnimate']);
            app.config(function($routeProvider) {

                $routeProvider

                    // home page
                    .when('/', {
                        templateUrl: 'page-home.html',
                        controller: 'mainController'
                    })

                    // about page
                    .when('/about', {
                        templateUrl: 'page-about.html',
                        controller: 'aboutController'
                    })

                    // contact page
                    .when('/contact', {
                        templateUrl: 'page-contact.html',
                        controller: 'contactController'
                    });

            });

            app.controller('mainController', function($scope) {
                $scope.pageClass = 'page-home';
            });

            // about page controller
            app.controller('aboutController', function($scope) {
                $scope.pageClass = 'page-about';
            });

            // contact page controller
            app.controller('contactController', function($scope) {
                $scope.pageClass = 'page-contact';
            });
        </script>
        <style type="text/css">
            .page-home {
                background:#00D0BC; color:#00907c;
            }

            .page-about {
                background:#E59400; color:#a55400;
            }

            .page-contact {
               background:#ffa6bb; color:#9e0000;
            }

            .page {
                bottom:0;
                padding-top:200px;
                position:absolute;
                text-align:center;
                top:0;  
                width:100%;
            }

            .page h1 {
                font-size:60px;
            }

            .page a {
                margin-top:50px;
            }

            /* ANIMATIONS
            ============================================================================= */

            /* leaving animations ----------------------------------------- */
            /* rotate and fall */
            @keyframes rotateFall {
                0%      { transform: rotateZ(0deg); }
                20%     { transform: rotateZ(10deg); animation-timing-function: ease-out; }
                40%     { transform: rotateZ(17deg); }
                60%     { transform: rotateZ(16deg); }
                100%    { transform: translateY(100%) rotateZ(17deg); }
            }

            /* slide in from the bottom */
            @keyframes slideOutLeft {
                to      { transform: translateX(-100%); }
            }

            /* rotate out newspaper */
            @keyframes rotateOutNewspaper {
                to      { transform: translateZ(-3000px) rotateZ(360deg); opacity: 0; }
            }

            /* entering animations --------------------------------------- */
            /* scale up */
            @keyframes scaleUp {
                from    { opacity: 0.3; -webkit-transform: scale(0.8); }
            }

            /* slide in from the right */
            @keyframes slideInRight {
                from    { transform:translateX(100%); }
                to      { transform: translateX(0); }
            }

            /* slide in from the bottom */
            @keyframes slideInUp {
                from    { transform:translateY(100%); }
                to      { transform: translateY(0); }
            }

            .ng-enter { animation: scaleUp 0.5s both ease-in; z-index: 8888; }
            .ng-leave { animation: rotateFall 0.5s both ease-in; z-index: 9999; }

            /*.ng-enter       { z-index: 8888; }
            .ng-leave       { z-index: 9999; }*/

            /* page specific animations ------------------------ */

            /* home -------------------------- */
            /*.page-home.ng-enter         { animation: scaleUp 0.5s both ease-in; }
            .page-home.ng-leave         { transform-origin: 0% 0%; animation: rotateFall 1s both ease-in; }*/

            /* about ------------------------ */
            /*.page-about.ng-enter        { animation:slideInRight 0.5s both ease-in; }
            .page-about.ng-leave        { animation:slideOutLeft 0.5s both ease-in; }*/

            /* contact ---------------------- */
            /*.page-contact.ng-leave      { transform-origin: 50% 50%; animation: rotateOutNewspaper .5s both ease-in; }
            .page-contact.ng-enter      { animation:slideInUp 0.5s both ease-in; }*/
        </style>
    </head>
    <body ng-app="myapp">
        <div class="page {{ pageClass }}" ng-view></div>
    </body>

</html>


page-home.html

<h1>Angular Animations</h1>
<h2>Home</h2>

<a href="#about" class="btn btn-success btn-lg">About</a>
<a href="#contact" class="btn btn-danger btn-lg">Contact</a>


page-about.html

<h1>Animating Pages Is Fun</h1>
<h2>About</h2>

<a href="#/" class="btn btn-primary btn-lg">Home</a>
<a href="#contact" class="btn btn-danger btn-lg">Contact</a>


page-contact.html

<h1>Tons of Animations</h1>
<h2>Contact</h2>

<a href="#/" class="btn btn-primary btn-lg">Home</a>
<a href="#about" class="btn btn-success btn-lg">About</a>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值