animate.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { canIUseAnimate } from '../common/version';
  2. import { getRect } from '../common/utils';
  3. function useAnimate(context, expanded, mounted, height) {
  4. const selector = '.van-collapse-item__wrapper';
  5. if (expanded) {
  6. context.animate(
  7. selector,
  8. [
  9. { height: 0, ease: 'ease-in-out', offset: 0 },
  10. { height: `${height}px`, ease: 'ease-in-out', offset: 1 },
  11. { height: `auto`, ease: 'ease-in-out', offset: 1 },
  12. ],
  13. mounted ? 300 : 0,
  14. () => {
  15. context.clearAnimation(selector);
  16. }
  17. );
  18. return;
  19. }
  20. context.animate(
  21. selector,
  22. [
  23. { height: `${height}px`, ease: 'ease-in-out', offset: 0 },
  24. { height: 0, ease: 'ease-in-out', offset: 1 },
  25. ],
  26. 300,
  27. () => {
  28. context.clearAnimation(selector);
  29. }
  30. );
  31. }
  32. function useAnimation(context, expanded, mounted, height) {
  33. const animation = wx.createAnimation({
  34. duration: 0,
  35. timingFunction: 'ease-in-out',
  36. });
  37. if (expanded) {
  38. if (height === 0) {
  39. animation.height('auto').top(1).step();
  40. } else {
  41. animation
  42. .height(height)
  43. .top(1)
  44. .step({
  45. duration: mounted ? 300 : 1,
  46. })
  47. .height('auto')
  48. .step();
  49. }
  50. context.setData({
  51. animation: animation.export(),
  52. });
  53. return;
  54. }
  55. animation.height(height).top(0).step({ duration: 1 }).height(0).step({
  56. duration: 300,
  57. });
  58. context.setData({
  59. animation: animation.export(),
  60. });
  61. }
  62. export function setContentAnimate(context, expanded, mounted) {
  63. getRect(context, '.van-collapse-item__content')
  64. .then((rect) => rect.height)
  65. .then((height) => {
  66. canIUseAnimate()
  67. ? useAnimate(context, expanded, mounted, height)
  68. : useAnimation(context, expanded, mounted, height);
  69. });
  70. }