transition.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const elTransition = '0.3s height ease-in-out, 0.3s padding-top ease-in-out, 0.3s padding-bottom ease-in-out'
  2. const Transition = {
  3. 'before-enter' (el) {
  4. el.style.transition = elTransition
  5. if (!el.dataset) el.dataset = {}
  6. el.dataset.oldPaddingTop = el.style.paddingTop
  7. el.dataset.oldPaddingBottom = el.style.paddingBottom
  8. el.style.height = 0
  9. el.style.paddingTop = 0
  10. el.style.paddingBottom = 0
  11. },
  12. 'enter' (el) {
  13. el.dataset.oldOverflow = el.style.overflow
  14. if (el.scrollHeight !== 0) {
  15. el.style.height = el.scrollHeight + 'px'
  16. el.style.paddingTop = el.dataset.oldPaddingTop
  17. el.style.paddingBottom = el.dataset.oldPaddingBottom
  18. } else {
  19. el.style.height = ''
  20. el.style.paddingTop = el.dataset.oldPaddingTop
  21. el.style.paddingBottom = el.dataset.oldPaddingBottom
  22. }
  23. el.style.overflow = 'hidden'
  24. },
  25. 'after-enter' (el) {
  26. el.style.transition = ''
  27. el.style.height = ''
  28. el.style.overflow = el.dataset.oldOverflow
  29. },
  30. 'before-leave' (el) {
  31. if (!el.dataset) el.dataset = {}
  32. el.dataset.oldPaddingTop = el.style.paddingTop
  33. el.dataset.oldPaddingBottom = el.style.paddingBottom
  34. el.dataset.oldOverflow = el.style.overflow
  35. el.style.height = el.scrollHeight + 'px'
  36. el.style.overflow = 'hidden'
  37. },
  38. 'leave' (el) {
  39. if (el.scrollHeight !== 0) {
  40. el.style.transition = elTransition
  41. el.style.height = 0
  42. el.style.paddingTop = 0
  43. el.style.paddingBottom = 0
  44. }
  45. },
  46. 'after-leave' (el) {
  47. el.style.transition = ''
  48. el.style.height = ''
  49. el.style.overflow = el.dataset.oldOverflow
  50. el.style.paddingTop = el.dataset.oldPaddingTop
  51. el.style.paddingBottom = el.dataset.oldPaddingBottom
  52. }
  53. }
  54. export default {
  55. name: 'collapseTransition',
  56. functional: true,
  57. render (h, { children }) {
  58. const data = {
  59. on: Transition
  60. }
  61. return h('transition', data, children)
  62. }
  63. }