Vue <TransitionGroup> 组件

实例

使用内置的 <TransitionGroup> 组件创建一个 <ol> 标签,其中包含动画 <li> 标签。

  1. <TransitionGroup tag="ol">
  2. <li v-for="x in products" :key="x">
  3. {{ x }}
  4. </li>
  5. </TransitionGroup>

定义与用法

内置的 <TransitionGroup> 组件用于使用 v-for 创建的元素,以便在添加或删除这些元素时为其提供单独的动画。

<TransitionGroup> 组件内使用 v-for 创建的标签必须使用 key 属性进行唯一定义。

只有当我们使用标签 prop 将 <TransitionGroup> 组件定义为特定标签时,它才会被呈现为 HTML 标签。

当在 <TransitionGroup> 组件内使用 v-for 基于数组创建标签时,当元素添加到数组或从数组中删除时,这些标签将自动设置动画。


Props

内置 <TransitionGroup> 组件可以与内置 <Transition> 组件使用相同的 prop:

Prop描述试一试
none默认。试一试
tag<TransitionGroup> 呈现为指定的标签。如果未设置标签 prop,则 <TransitionGroup> 将不会呈现为标签。试一试
moveClassmove 类创建一个自定义的名称。默认名称是 v-move试一试

更多实例

实例 1

骰子可以添加或移除,添加的骰子使用 <TransitionGroup> 设置动画:

  1. <template>
  2. <h3>The <TransitionGroup> Component</h3>
  3. <p>New products are given animations using the <TransitionGroup> component.</p>
  4. <button @click="addDie">Roll</button>
  5. <button @click="removeDie">Remove random</button><br>
  6. <TransitionGroup>
  7. <div v-for="x in dice" :key="x" class="diceDiv" :style="{ backgroundColor: 'hsl('+x*40+',85%,85%)' }">
  8. {{ x }}
  9. </div>
  10. </TransitionGroup>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. dice: [],
  17. inpName: ''
  18. }
  19. },
  20. methods: {
  21. addDie() {
  22. const newDie = Math.ceil(Math.random()*6);
  23. this.dice.push(newDie);
  24. },
  25. removeDie() {
  26. if(this.dice.length>0){
  27. this.dice.splice(Math.floor(Math.random()*this.dice.length), 1);
  28. }
  29. }
  30. },
  31. mounted() {
  32. this.addDie();
  33. this.addDie();
  34. this.addDie();
  35. }
  36. }
  37. </script>
  38. <style>
  39. .v-enter-from {
  40. opacity: 0;
  41. translate: 200px 0;
  42. rotate: 360deg;
  43. }
  44. .v-enter-to {
  45. opacity: 1;
  46. translate: 0 0;
  47. rotate: 0deg;
  48. }
  49. .v-enter-active,
  50. .v-leave-active {
  51. transition: all 0.7s;
  52. }
  53. .v-leave-from { opacity: 1; }
  54. .v-leave-to { opacity: 0; }
  55. .diceDiv {
  56. margin: 10px;
  57. width: 30px;
  58. height: 30px;
  59. line-height: 30px;
  60. vertical-align: middle;
  61. text-align: center;
  62. border: solid black 1px;
  63. border-radius: 5px;
  64. display: inline-block;
  65. }
  66. </style>
实例 2

骰子可以添加或移除,添加和移除的骰子都使用 <TransitionGroup> 设置动画:

  1. <template>
  2. <h3>The <TransitionGroup> Component</h3>
  3. <p>When an item is removed inside the <TransitionGroup> component, other items are animated as they fall into their new positions.</p>
  4. <button @click="addDie">Roll</button>
  5. <button @click="removeDie">Remove random</button><br>
  6. <TransitionGroup>
  7. <div v-for="x in dice" :key="x" class="diceDiv" :style="{ backgroundColor: 'hsl('+x*40+',85%,85%)' }">
  8. {{ x }}
  9. </div>
  10. </TransitionGroup>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. dice: [],
  17. inpName: ''
  18. }
  19. },
  20. methods: {
  21. addDie() {
  22. const newDie = Math.ceil(Math.random()*6);
  23. this.dice.push(newDie);
  24. },
  25. removeDie() {
  26. if(this.dice.length>0){
  27. this.dice.splice(Math.floor(Math.random()*this.dice.length), 1);
  28. }
  29. }
  30. },
  31. mounted() {
  32. this.addDie();
  33. this.addDie();
  34. this.addDie();
  35. }
  36. }
  37. </script>
  38. <style>
  39. .v-enter-from {
  40. opacity: 0;
  41. translate: 200px 0;
  42. rotate: 360deg;
  43. }
  44. .v-enter-to {
  45. opacity: 1;
  46. translate: 0 0;
  47. rotate: 0deg;
  48. }
  49. .v-enter-active,
  50. .v-leave-active,
  51. .v-move {
  52. transition: all 0.7s;
  53. }
  54. .v-leave-active { position: absolute; }
  55. .v-leave-from { opacity: 1; }
  56. .v-leave-to { opacity: 0; }
  57. .diceDiv {
  58. margin: 10px;
  59. width: 30px;
  60. height: 30px;
  61. line-height: 30px;
  62. vertical-align: middle;
  63. text-align: center;
  64. border: solid black 1px;
  65. border-radius: 5px;
  66. display: inline-block;
  67. }
  68. </style>
实例 3

骰子可以添加、移除、洗牌或排序,所有动画都使用 <TransitionGroup>

  1. <template>
  2. <h3>The <TransitionGroup> Component</h3>
  3. <p>Items inside the <TransitionGroup> component are animated when they are created or removed.</p>
  4. <button @click="addDie">Roll</button>
  5. <button @click="addDie10">Roll 10 dice</button>
  6. <button @click="dice.sort(compareFunc)">Sort</button>
  7. <button @click="dice.sort(shuffleFunc)">Shuffle</button><br>
  8. <TransitionGroup>
  9. <div
  10. v-for="x in dice"
  11. :key="x.keyNmbr"
  12. class="diceDiv"
  13. :style="{ backgroundColor: 'hsl('+x.dieNmbr*60+',85%,85%)' }"
  14. @click="removeDie(x.keyNmbr)">
  15. {{ x.dieNmbr }}
  16. </div>
  17. </TransitionGroup>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. dice: [],
  24. keyNumber: 0
  25. }
  26. },
  27. methods: {
  28. addDie() {
  29. const newDie = {
  30. dieNmbr: Math.ceil(Math.random()*6),
  31. keyNmbr: this.keyNumber
  32. };
  33. this.dice.splice(Math.floor(Math.random()*this.dice.length),0,newDie);
  34. this.keyNumber++;
  35. },
  36. addDie10() {
  37. for(let i=0; i<10; i++) {
  38. this.addDie();
  39. }
  40. },
  41. compareFunc(a,b){
  42. return a.dieNmbr - b.dieNmbr;
  43. },
  44. shuffleFunc(a,b){
  45. return Math.random()-0.5;
  46. },
  47. removeDie(key) {
  48. const pos = this.dice.map(e => e.keyNmbr).indexOf(key);
  49. this.dice.splice(pos, 1);
  50. }
  51. },
  52. mounted() {
  53. this.addDie10();
  54. }
  55. }
  56. </script>
  57. <style>
  58. .v-enter-from {
  59. opacity: 0;
  60. scale: 0;
  61. rotate: 360deg;
  62. }
  63. .v-enter-to {
  64. opacity: 1;
  65. scale: 1;
  66. rotate: 0deg;
  67. }
  68. .v-enter-active,
  69. .v-leave-active,
  70. .v-move {
  71. transition: all 0.7s;
  72. }
  73. .v-leave-active { position: absolute; }
  74. .v-leave-from { opacity: 1; }
  75. .v-leave-to { opacity: 0; }
  76. .diceDiv {
  77. margin: 10px;
  78. width: 30px;
  79. height: 30px;
  80. line-height: 30px;
  81. vertical-align: middle;
  82. text-align: center;
  83. border: solid black 1px;
  84. border-radius: 5px;
  85. display: inline-block;
  86. }
  87. .diceDiv:hover {
  88. cursor: pointer;
  89. box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  90. }
  91. #app {
  92. position: relative;
  93. }
  94. </style>
实例 4

使用 moveClass 重命名 move 类:

  1. <template>
  2. <h3>The <TransitionGroup> Component</h3>
  3. <p>When an item is removed inside the <TransitionGroup> component, other items are animated as they fall into their new positions.</p>
  4. <button @click="addDie">Roll</button>
  5. <button @click="removeDie">Remove random</button><br>
  6. <TransitionGroup moveClass="good-slide">
  7. <div
  8. v-for="x in dice"
  9. :key="x"
  10. class="diceDiv"
  11. :style="{ backgroundColor: 'hsl('+x*40+',85%,85%)' }"
  12. >
  13. {{ x }}
  14. </div>
  15. </TransitionGroup>
  16. </template>
  17. <script>
  18. export default {
  19. data() {
  20. return {
  21. dice: [],
  22. inpName: ''
  23. }
  24. },
  25. methods: {
  26. addDie() {
  27. const newDie = Math.ceil(Math.random()*6);
  28. this.dice.push(newDie);
  29. },
  30. removeDie() {
  31. if(this.dice.length>0){
  32. this.dice.splice(Math.floor(Math.random()*this.dice.length), 1);
  33. }
  34. }
  35. },
  36. mounted() {
  37. this.addDie();
  38. this.addDie();
  39. this.addDie();
  40. }
  41. }
  42. </script>
  43. <style>
  44. .v-enter-from {
  45. opacity: 0;
  46. translate: 200px 0;
  47. rotate: 360deg;
  48. }
  49. .v-enter-to {
  50. opacity: 1;
  51. translate: 0 0;
  52. rotate: 0deg;
  53. }
  54. .v-enter-active,
  55. .v-leave-active,
  56. .good-slide {
  57. transition: all 0.7s;
  58. }
  59. .v-leave-active { position: absolute; }
  60. .v-leave-from { opacity: 1; }
  61. .v-leave-to { opacity: 0; }
  62. .diceDiv {
  63. margin: 10px;
  64. width: 30px;
  65. height: 30px;
  66. line-height: 30px;
  67. vertical-align: middle;
  68. text-align: center;
  69. border: solid black 1px;
  70. border-radius: 5px;
  71. display: inline-block;
  72. }
  73. </style>

分类导航