• We just launched and are currently in beta. Join us as we build and grow the community.

Angular JS Simple Slide Animation using ngAnimate with Ui-Router

aweatherbornestorm

Combo Breaker
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
59
Likes
115
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Getting Started

I've used CDN for Bootstrap, Angular JS, and Angular Animate so you need internet connection for them to work.

index.html

This is the main view of our app.

  1. <!DOCTYPE html>
  2. <html

    ng-app=

    "app"

    >
  3. <head

    >
  4. <title

    >

    Angular JS Toggle Active Class on Path Change using Ui-Router</

    title

    >
  5. <meta

    charset

    =

    "utf-8"

    >
  6. <link

    href

    =

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"

    rel

    =

    "stylesheet"

    >
  7. <script

    src

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"

    ></

    script

    >
  8. <script

    src

    =

    "https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js"

    ></

    script

    >
  9. <script

    src

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-animate.min.js"

    ></

    script

    >
  10. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "style.css"

    >
  11. </

    head

    >
  12. <body

    ng-controller=

    "mainCtrl"

    >
  13. <nav class

    =

    "navbar navbar-default"

    >
  14. <div

    class

    =

    "container"

    >
  15. <div

    class

    =

    "navbar-header"

    >
  16. <button

    type

    =

    "button"

    class

    =

    "navbar-toggle collapsed"

    data-toggle=

    "collapse"

    data-target

    =

    "#bs-example-navbar-collapse-1"

    aria-expanded=

    "false"

    >
  17. <span

    class

    =

    "sr-only"

    >

    Toggle navigation</

    span

    >
  18. <span

    class

    =

    "icon-bar"

    ></

    span

    >
  19. <span

    class

    =

    "icon-bar"

    ></

    span

    >
  20. <span

    class

    =

    "icon-bar"

    ></

    span

    >
  21. </

    button

    >
  22. <a

    class

    =

    "navbar-brand"

    href

    =

    "#"

    >

    SourceCodeSter</

    a

    >
  23. </

    div

    >
  24. <div

    class

    =

    "collapse navbar-collapse"

    id

    =

    "bs-example-navbar-collapse-1"

    >
  25. <ul

    class

    =

    "nav navbar-nav"

    >
  26. <li

    ng-class

    =

    "active('/home')"

    ><a

    ui-sref=

    "home"

    >

    Home</

    a

    ></

    li

    >
  27. <li

    ng-class

    =

    "active('/about')"

    ><a

    ui-sref=

    "about"

    >

    About</

    a

    ></

    li

    >
  28. <li

    ng-class

    =

    "active('/blog')"

    ><a

    ui-sref=

    "blog"

    >

    Blog</

    a

    ></

    li

    >
  29. </

    ul

    >
  30. </

    div

    >
  31. </

    div

    >
  32. </

    nav>
  33. <div

    class

    =

    "container"

    >
  34. <div

    id

    =

    "views"

    ui-view></

    div

    >
  35. </

    div

    >
  36. <script

    src

    =

    "app.js"

    ></

    script

    >
  37. </

    body

    >
  38. </

    html

    >

In here we have used Bootstrap navbar to create our menus and we assign id for our ui-view where we add our animation CSS.

Add Style

Add the ff CSS to trigger our slide animation. We name this as style.css
  1. #views

    .ng-enter,
  2. #views

    .ng-leave

    {
  3. position

    :

    absolute

    ;

    left

    :

    30px

    ;

    right

    :

    30px

    ;
  4. transition

    :

    0.5s

    all ease;

    -moz-transition:

    0.5s

    all ease;

    -webkit-transition:

    0.5s

    all ease;
  5. }

  6. #views

    .ng-enter

    {
  7. -webkit-animation:

    slideInRight 0.5s

    both

    ease;
  8. -moz-animation:

    slideInRight 0.5s

    both

    ease;
  9. animation

    :

    slideInRight 0.5s

    both

    ease;
  10. }

  11. #views

    .ng-leave

    {
  12. -webkit-animation:

    slideOutLeft 0.5s

    both

    ease;
  13. -moz-animation:

    slideOutLeft 0.5s

    both

    ease;
  14. animation

    :

    slideOutLeft 0.5s

    both

    ease;
  15. }

  16. @keyframes

    slideOutLeft {
  17. to {

    transform

    :

    translateX(

    -200%

    )

    ;

    }
  18. }
  19. @-moz-keyframes

    slideOutLeft {
  20. to {

    -moz-transform:

    translateX(

    -200%

    )

    ;

    }
  21. }
  22. @-webkit-keyframes

    slideOutLeft {
  23. to {

    -webkit-transform:

    translateX(

    -200%

    )

    ;

    }
  24. }

  25. @keyframes

    slideInRight {
  26. from {

    transform

    :

    translateX(

    200%

    )

    ;

    }
  27. to {

    transform

    :

    translateX(

    0

    )

    ;

    }
  28. }
  29. @-moz-keyframes

    slideInRight {
  30. from {

    -moz-transform:

    translateX(

    200%

    )

    ;

    }
  31. to {

    -moz-transform:

    translateX(

    0

    )

    ;

    }
  32. }
  33. @-webkit-keyframes

    slideInRight {
  34. from {

    -webkit-transform:

    translateX(

    200%

    )

    ;

    }
  35. to {

    -webkit-transform:

    translateX(

    0

    )

    ;

    }
  36. }

app.js

This contains our angular js script.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    'ui.router'

    ,

    'ngAnimate'

    ]

    )

    ;

  2. app.config

    (

    function

    (

    $stateProvider,

    $urlRouterProvider)

    {

  3. $urlRouterProvider.otherwise

    (

    '/home'

    )

    ;

  4. $stateProvider

  5. .state

    (

    'home'

    ,

    {
  6. url:

    '/home'

    ,
  7. templateUrl:

    'home.html'
  8. }

    )

  9. .state

    (

    'about'

    ,

    {
  10. url:

    '/about'

    ,
  11. templateUrl:

    'about.html'

    ,

  12. }

    )

  13. .state

    (

    'blog'

    ,

    {
  14. url:

    '/blog'

    ,
  15. templateUrl:

    'blog.html'

    ,
  16. }

    )

    ;

  17. }

    )

    ;

  18. app.controller

    (

    'mainCtrl'

    ,

    function

    (

    $scope,

    $location)

    {
  19. $scope.active

    =

    function

    (

    path)

    {
  20. return

    (

    $location.path

    (

    )

    ===

    path)

    ?

    'active'

    :

    ''

    ;
  21. }
  22. }

    )

    ;

Notice that we have included 'ng-Animate' as the dependency for Angular Animate.

Add Path Templates

Add the ff templates for our path.

home.html
  1. <h4

    >

    This is the Home Page</

    h4

    >
  2. <p

    >

    See that the <b

    >

    Home Tab</

    b

    >

    is active</

    p

    >

about.html
  1. <h4

    >

    This is the About Page</

    h4

    >
  2. <p

    >

    See that the <b

    >

    About Tab</

    b

    >

    is active</

    p

    >

blog.html
  1. <h4

    >

    This is the Blog Page</

    h4

    >
  2. <p

    >

    See that the <b

    >

    Blog Tab</

    b

    >

    is active</

    p

    >

That ends this tutorial. Happy Coding :)

 

452,292

323,341

323,350

Top