动画绑定

ms-effect拥有这三种绑定形式:

  1. <p ms-effect="[@configObj,{is:'fade'}">属性值为字面量,其中一个对象必须包括is属性,这用于指定特效名</p>
  2. <p ms-effect="{is:fade, stagger:300}">属性值为对象字面量, 里面拥有is属性</p>
  3. <p ms-effect="@fadeConfig">属性值为vm的对象,里面拥有is属性</p>

avalon2实际上没有实现完整的动画模块,它只是对现有的CSS3动画或jquery animate再包装一层。

我们先说如何用CSS3为avalon实现动画效果。首先要使用avalon.effect注册一个特效。

  1. avalon.effect(name, definition)

所有注册了的特效,都可以在avalon.effects对象中找到。

css3动画要求我们至少添加4个类名。这个是从angular那里学过来的。因此如何你以前的项目是基于angular,它那些CSS动画类可以原封不动地搬过来用。

  1. avalon.effect('animate', {
  2. enterClass: 'animate-enter',
  3. enterActiveClass: 'animate-enter-active',
  4. leaveClass: 'animate-leave',
  5. leaveActiveClass: 'animate-leave-active',
  6. })

当然,这些类名会默认帮你添加,因为它内部是这样实现的。

  1. avalon.effect = function (name, opts) {
  2. var definition = avalon.effects[name] = (opts || {})
  3. if (support.css && definition.css !== false) {
  4. patchObject(definition, 'enterClass', name + '-enter')
  5. patchObject(definition, 'enterActiveClass', definition.enterClass + '-active')
  6. patchObject(definition, 'leaveClass', name + '-leave')
  7. patchObject(definition, 'leaveActiveClass', definition.leaveClass + '-active')
  8. }
  9. patchObject(definition, 'action', 'enter')
  10. }
  11. function patchObject(obj, name, value) {
  12. if (!obj[name]) {
  13. obj[name] = value
  14. }
  15. }

因此你可以简化成这样:

  1. avalon.effect('animate', {})
  2. avalon.effect('animate')

definition配置对象css如果等于false,那么它会强制使用JS方式

注册完,我们就需要在样式表中添加真正的CSS类。

  1. .animate-enter, .animate-leave{
  2. width:100px;
  3. height:100px;
  4. background: #29b6f6;
  5. transition: width 2s;
  6. -moz-transition: width 2s; /* Firefox 4 */
  7. -webkit-transition: width 2s; /* Safari 和 Chrome */
  8. -o-transition: width 2s; /* Opera */
  9. }
  10. .animate-enter-active, .animate-leave{
  11. width:300px;
  12. }
  13. .animate-leave-active{
  14. width:100px;
  15. }

我们还得定义一个vm,里面指明动画的动作(默认有三种方式, enter, leave, move) 及动画结束时的回调(这是可选的)

  1. var vm = avalon.define({
  2. $id: 'effect',
  3. aaa: "test",
  4. action: 'enter',
  5. enterCb: function(){
  6. avalon.log('动画完成')
  7. },
  8. leaveCb: function(){
  9. avalon.log('动画回到原点')
  10. }
  11. })

然后页面上这样使用:

  1. <div ms-controller='effect' >
  2. <div ms-effect="{is:'animate', action:@action,onEnterDone: @enterCb,onLeaveDone: @leaveCb}">
  3. {{@aaa}}
  4. </div>
  5. <button ms-click='@action = @action !== "leave" ? "leave": "enter"'
  6. type="button">click</button>
  7. </div>

ms-effect的值为一个对象,其中is是必选。除了action, 还支持这么多种回调:

  1. onEnterDone, onLeaveDone, onEnterAbort, onLeaveAbort, onBeforeEnter, onBeforeLeave

ms-effect  - 图1

如果使用JS实现,则是这样的:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>TODO supply a title</title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <script src="../dist/avalon.js"></script>
  8. <script src="//cdn.bootcss.com/jquery/3.0.0-beta1/jquery.js"></script>
  9. <style>
  10. .ani{
  11. width:100px;
  12. height:100px;
  13. background: #29b6f6;
  14. }
  15. </style>
  16. <script>
  17. avalon.effect('animate', {
  18. enter: function(el, done){
  19. $(el).animate({width: 300},1000,done)
  20. },
  21. leave: function(el, done){
  22. $(el).animate({width: 100},1000,done)
  23. }
  24. })
  25. var vm = avalon.define({
  26. $id: 'effect',
  27. aaa: "effect",
  28. action: 'enter',
  29. enterCb: function(){
  30. avalon.log('动画完成')
  31. },
  32. leaveCb: function(){
  33. avalon.log('动画回到原点')
  34. }
  35. })
  36. </script>
  37. </head>
  38. <body>
  39. <div ms-controller='effect' >
  40. <div class='ani' ms-effect="{is:'animate', action:@action,onEnterDone: @enterCb,onLeaveDone: @leaveCb}">
  41. {{@aaa}}
  42. </div>
  43. <button ms-click='@action = @action !== "leave" ? "leave": "enter"'
  44. type="button">click</button>
  45. </div>
  46. </body>
  47. </html>

一个CSS3位置效果

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>TODO supply a title</title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <script src="../dist/avalon.js"></script>
  8. <script src="//cdn.bootcss.com/jquery/3.0.0-beta1/jquery.js"></script>
  9. <style>
  10. .ani{
  11. width:100px;
  12. height:100px;
  13. background: #ff6e6e;
  14. }
  15. .wave-enter, .wave-leave {
  16. -webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
  17. -moz-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
  18. -o-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
  19. transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
  20. }
  21. .wave-enter {
  22. position:absolute;
  23. left:45%;
  24. }
  25. .wave-enter-active {
  26. left:0;
  27. }
  28. .wave-leave {
  29. position:absolute;
  30. left:0;
  31. }
  32. .wave-leave-active {
  33. left:45%;
  34. }
  35. </style>
  36. <script>
  37. avalon.effect('wave', {})
  38. var vm = avalon.define({
  39. $id: 'effect',
  40. action: 'enter',
  41. enterCb: function () {
  42. avalon.log('动画完成')
  43. },
  44. leaveCb: function () {
  45. avalon.log('动画回到原点')
  46. }
  47. })
  48. </script>
  49. </head>
  50. <body>
  51. <div ms-controller='effect' >
  52. <div class='ani' ms-effect="{is:'wave', action:@action,onEnterDone: @enterCb,onLeaveDone: @leaveCb}">
  53. <button ms-click='@action = @action !== "leave" ? "leave": "enter"'
  54. type="button">click</button>
  55. </div>
  56. </div>
  57. </body>
  58. </html>
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="format-detection" content="telephone=no">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no">
  7. <style>
  8. .animate-enter, .animate-leave{
  9. width:100px;
  10. height:100px;
  11. background: #29b6f6;
  12. transition: width 2s;
  13. -moz-transition: width 2s; /* Firefox 4 */
  14. -webkit-transition: width 2s; /* Safari 和 Chrome */
  15. -o-transition: width 2s; /* Opera */
  16. }
  17. .animate-enter-active, .animate-leave{
  18. width:300px;
  19. }
  20. .animate-leave-active{
  21. width:100px;
  22. }
  23. </style>
  24. <title></title>
  25. </head>
  26. <body ms-controller="body">
  27. <button ms-click="@toggle">toggle</button>
  28. <div ms-effect="{is : 'animate',action: @action}"></div>
  29. <script src="../../dist/avalon.js"></script>
  30. <script>
  31. avalon.effect("animate",{
  32. onEnterDone: function(){
  33. avalon.log('enter done')
  34. },
  35. onLeaveDone: function(){
  36. avalon.log('leave done')
  37. }
  38. });
  39. var vm = avalon.define({
  40. $id : "body",
  41. action: 'enter',
  42. toggle : function(){
  43. vm.action = vm.action === 'enter' ? 'leave' : 'enter'
  44. avalon.log('toggle', vm.action)
  45. }
  46. });
  47. </script>
  48. </body>
  49. </html>

ms-widget+ms-for+ms-if+ms-effect的动画效果

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>ms-if</title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width">
  7. <script src="../dist/avalon.js"></script>
  8. <script src="//cdn.bootcss.com/jquery/3.0.0-beta1/jquery.js"></script>
  9. <style>
  10. .ani{
  11. width:100px;
  12. height:100px;
  13. background: #ff6e6e;
  14. }
  15. </style>
  16. <script >
  17. avalon.component('ms-button', {
  18. template: '<button type="button"><span><slot name="buttonText"></slot></span></button>',
  19. defaults: {
  20. buttonText: "button"
  21. },
  22. soleSlot: 'buttonText'
  23. })
  24. avalon.effect('zoom', {
  25. enter: function (el, done) {
  26. $(el).css({width: 0, height: 0}).animate({
  27. width: 100, height: 100
  28. }, 1000, done)
  29. },
  30. leave: function (el, done) {
  31. $(el).animate({
  32. width: 0, height: 0
  33. }, 1000, done)
  34. }
  35. })
  36. var vm = avalon.define({
  37. $id: "effect1",
  38. arr: [1,2,3],
  39. aaa: 222,
  40. toggle: true
  41. })
  42. </script>
  43. </head>
  44. <body ms-controller="test" >
  45. <div ms-for="el in @arr">
  46. <div class='ani'
  47. ms-attr="{eee:el}"
  48. ms-if="@toggle"
  49. ms-widget='{is:"ms-button"}'
  50. ms-effect="{is:'zoom'}">{{@aaa}}::{{el}}</div>
  51. </div>
  52. <button ms-click="@toggle = !@toggle " >点我 </button >
  53. </body>
  54. </html>

ms-for与stagger的动画效果

这次为了与angular一致,stagger改为一个小数,它会让当前元素延迟stagger秒执行。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <script src="../dist/avalon.js"></script>
  7. <style>
  8. .my-repeat-animation {
  9. width: 400px;
  10. height: 30px;
  11. -webkit-animation-duration: 1s;
  12. animation-duration: 1s;
  13. }
  14. .ng-enter {
  15. -webkit-animation-name: enter_animation;
  16. animation-name: enter_animation;
  17. }
  18. .ng-enter-stagger {
  19. animation-delay:300ms;
  20. -webkit-animation-delay:300ms;
  21. }
  22. .ng-leave {
  23. -webkit-animation-name: leave_animation;
  24. animation-name: leave_animation;
  25. }
  26. @keyframes enter_animation {
  27. 0% {
  28. opacity: 0;
  29. }
  30. 100% {
  31. opacity: 1;
  32. }
  33. }
  34. @keyframes leave_animation {
  35. from {
  36. opacity: 1;
  37. }
  38. to {
  39. opacity: 0;
  40. }
  41. }
  42. @-webkit-keyframes enter_animation {
  43. from {
  44. opacity: 0;
  45. }
  46. to {
  47. opacity: 1;
  48. }
  49. }
  50. @-webkit-keyframes leave_animation {
  51. from {
  52. opacity: 1;
  53. }
  54. to {
  55. opacity: 0;
  56. }
  57. }
  58. </style>
  59. <script>
  60. avalon.effect("my-repeat-animation", {
  61. enterClass: "ng-enter",
  62. leaveClass: "ng-leave"
  63. })
  64. var vm = avalon.define({
  65. $id: "test",
  66. array: [1, 2, 3, 4],
  67. getBg: function() {
  68. return '#' + Math.floor(Math.random() * 16777215).toString(16);
  69. },
  70. add: function() {
  71. vm.array.push(vm.array.length + 1)
  72. vm.array.push(vm.array.length + 1)
  73. vm.array.push(vm.array.length + 1)
  74. vm.array.push(vm.array.length + 1)
  75. vm.array.push(vm.array.length + 1)
  76. vm.array.push(vm.array.length + 1)
  77. vm.array.push(vm.array.length + 1)
  78. vm.array.push(vm.array.length + 1)
  79. vm.array.push(vm.array.length + 1)
  80. },
  81. value: ""
  82. })
  83. vm.$watch("value", function(a) {
  84. if (a) {
  85. vm.array.removeAll(function(el) {
  86. return el !== a
  87. })
  88. } else {
  89. if(vm.array.length < 12)
  90. vm.add()
  91. }
  92. })
  93. </script>
  94. </head>
  95. <body ms-controller="test">
  96. <button ms-click="@add">Add</button>
  97. <input placeholder="只保留" ms-duplex-number="@value" />
  98. <div class="my-repeat-animation" ms-for="item in @array"
  99. ms-css="{background:@getBg()}"
  100. ms-effect="{is:'my-repeat-animation',stagger:0.3}">
  101. {{item}}
  102. </div>
  103. </body>
  104. </html>

目前,avalon的ms-effect可以与ms-visible,ms-if,ms-repeat连用。ms-effect也可以单独或与其他指令使用,这时需要你指定action。

  1. <div ms-effect="{is:"effectName", action: @action}">