list.html 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <script src="../../../../node_modules/lodash/lodash.min.js"></script>
  2. <script src="../../dist/vue.global.js"></script>
  3. <div id="app">
  4. <button @click="insert">insert at random index</button>
  5. <button @click="reset">reset</button>
  6. <button @click="shuffle">shuffle</button>
  7. <transition-group tag="ul" name="fade" class="container">
  8. <item v-for="item in items"
  9. class="item"
  10. :msg="item"
  11. :key="item"
  12. @rm="remove(item)">
  13. </item>
  14. </transition-group>
  15. </div>
  16. <script>
  17. const getInitialItems = () => [1, 2, 3, 4, 5]
  18. let id = getInitialItems().length + 1
  19. const Item = {
  20. props: ['msg'],
  21. template: `<div>{{ msg }} <button @click="$emit('rm')">x</button></div>`
  22. }
  23. Vue.createApp({
  24. components: {
  25. Item
  26. },
  27. data() {
  28. return {
  29. items: getInitialItems()
  30. }
  31. },
  32. methods: {
  33. insert () {
  34. const i = Math.round(Math.random() * this.items.length)
  35. this.items.splice(i, 0, id++)
  36. },
  37. reset () {
  38. this.items = getInitialItems()
  39. },
  40. shuffle () {
  41. this.items = _.shuffle(this.items)
  42. },
  43. remove (item) {
  44. const i = this.items.indexOf(item)
  45. if (i > -1) {
  46. this.items.splice(i, 1)
  47. }
  48. }
  49. }
  50. }).mount('#app')
  51. </script>
  52. <style>
  53. .container {
  54. position: relative;
  55. padding: 0;
  56. }
  57. .item {
  58. width: 100%;
  59. height: 30px;
  60. background-color: #f3f3f3;
  61. border: 1px solid #666;
  62. box-sizing: border-box;
  63. }
  64. /* 1. declare transition */
  65. .fade-move, .fade-enter-active, .fade-leave-active {
  66. transition: all .5s cubic-bezier(.55,0,.1,1);
  67. }
  68. /* 2. declare enter from and leave to state */
  69. .fade-enter-from, .fade-leave-to {
  70. opacity: 0;
  71. transform: scaleY(0.01) translate(30px, 0);
  72. }
  73. /* 3. ensure leaving items are taken out of layout flow so that moving
  74. animations can be calculated correctly. */
  75. .fade-leave-active {
  76. position: absolute;
  77. }
  78. </style>