Transition

Transition & Transition Group. 很容易运动一个或者一堆 dom 元素,提供优雅的进入或者离开过渡动画。

Usage

  • <m-transition></m-transition>
  • <m-transition-group></m-transition-group>

案例

  1. import { define, WeElement, render, h } from 'omi'
  2. import 'omim/transition'
  3. import 'omim/transition-group'
  4. define('my-app', class extends WeElement {
  5. static css = `
  6. .item-leave-to, .item-enter, .item-appear {
  7. opacity: 0;
  8. transform: translateX(15px);
  9. }
  10. .item-leave-active, .item-enter-active,.item-appear-active {
  11. transition: all 500ms ease-in;
  12. }`
  13. _id = 0
  14. items = [
  15. { _id: this._id++, text: 'Learn omi' },
  16. { _id: this._id++, text: 'Learn omim' },
  17. { _id: this._id++, text: 'Learn transition' },
  18. { _id: this._id++, text: 'Learn transition group' },
  19. ]
  20. toggle = () => {
  21. this.removed = false
  22. this.show = !this.show
  23. this.update()
  24. }
  25. onRemoved = () => {
  26. this.removed = true
  27. }
  28. show = true
  29. removed = false
  30. render() {
  31. const items = this.items
  32. return (
  33. <div style={{ marginTop: '2rem' }}>
  34. <h1>Transition</h1>
  35. <m-transition onRemoved={this.onRemoved} name="item" appear removable removed={this.removed} show={this.show}>
  36. <p>Hello transition</p>
  37. </m-transition>
  38. <br />
  39. <button onclick={this.toggle}>Toggle</button>
  40. <h1>Transition Group</h1>
  41. <ul>
  42. <m-transition-group name="item" appear delay={200}>
  43. {items.map(({ _id, text }) =>
  44. <li key={_id}>
  45. <button
  46. onClick={() => {
  47. this.items = items.filter(item => item._id !== _id)
  48. this.update()
  49. }}>
  50. &times;
  51. </button> {text}
  52. </li>
  53. )}
  54. </m-transition-group>
  55. </ul>
  56. <button
  57. onClick={() => {
  58. const text = prompt('Enter some text');
  59. if (text) {
  60. this.items.push({ _id: this._id++, text })
  61. this.update()
  62. }
  63. }}
  64. >
  65. Add Item
  66. </button>
  67. </div>
  68. )
  69. }
  70. })
  71. render(<my-app />, 'body')