collapse-transition.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. class Transition {
  2. beforeEnter(el) {
  3. if (!el.dataset) el.dataset = {};
  4. el.dataset.oldPaddingTop = el.style.paddingTop;
  5. el.dataset.oldPaddingBottom = el.style.paddingBottom;
  6. el.dataset.oldDisplay = el.style.display;
  7. el.style.height = '0';
  8. el.style.paddingTop = 0;
  9. el.style.paddingBottom = 0;
  10. }
  11. enter(el) {
  12. el.dataset.oldOverflow = el.style.overflow;
  13. el.style.display = 'block';
  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. afterEnter(el) {
  26. el.style.display = '';
  27. el.style.height = '';
  28. el.style.overflow = el.dataset.oldOverflow;
  29. }
  30. beforeLeave(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.display = 'block';
  36. if (el.scrollHeight !== 0) {
  37. el.style.height = el.scrollHeight + 'px';
  38. }
  39. el.style.overflow = 'hidden';
  40. }
  41. leave(el) {
  42. if (el.scrollHeight !== 0) {
  43. setTimeout(() => {
  44. el.style.height = 0;
  45. el.style.paddingTop = 0;
  46. el.style.paddingBottom = 0;
  47. });
  48. }
  49. }
  50. afterLeave(el) {
  51. el.style.display = el.dataset.oldDisplay;
  52. el.style.height = '';
  53. el.style.overflow = el.dataset.oldOverflow;
  54. el.style.paddingTop = el.dataset.oldPaddingTop;
  55. el.style.paddingBottom = el.dataset.oldPaddingBottom;
  56. }
  57. }
  58. export default {
  59. functional: true,
  60. render(h, { children }) {
  61. const data = {
  62. on: new Transition()
  63. };
  64. children = children.map(item => {
  65. item.data.class = ['collapse-transition'];
  66. return item;
  67. });
  68. return h('transition', data, children);
  69. }
  70. };