index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { getRect } from '../common/utils';
  2. import { VantComponent } from '../common/component';
  3. import { pageScrollMixin } from '../mixins/page-scroll';
  4. const ROOT_ELEMENT = '.van-sticky';
  5. VantComponent({
  6. props: {
  7. zIndex: {
  8. type: Number,
  9. value: 99,
  10. },
  11. offsetTop: {
  12. type: Number,
  13. value: 0,
  14. observer: 'onScroll',
  15. },
  16. disabled: {
  17. type: Boolean,
  18. observer: 'onScroll',
  19. },
  20. container: {
  21. type: null,
  22. observer: 'onScroll',
  23. },
  24. scrollTop: {
  25. type: null,
  26. observer(val) {
  27. this.onScroll({ scrollTop: val });
  28. },
  29. },
  30. },
  31. mixins: [
  32. pageScrollMixin(function (event) {
  33. if (this.data.scrollTop != null) {
  34. return;
  35. }
  36. this.onScroll(event);
  37. }),
  38. ],
  39. data: {
  40. height: 0,
  41. fixed: false,
  42. transform: 0,
  43. },
  44. mounted() {
  45. this.onScroll();
  46. },
  47. methods: {
  48. onScroll({ scrollTop } = {}) {
  49. const { container, offsetTop, disabled } = this.data;
  50. if (disabled) {
  51. this.setDataAfterDiff({
  52. fixed: false,
  53. transform: 0,
  54. });
  55. return;
  56. }
  57. this.scrollTop = scrollTop || this.scrollTop;
  58. if (typeof container === 'function') {
  59. Promise.all([
  60. getRect(this, ROOT_ELEMENT),
  61. this.getContainerRect(),
  62. ]).then(([root, container]) => {
  63. if (offsetTop + root.height > container.height + container.top) {
  64. this.setDataAfterDiff({
  65. fixed: false,
  66. transform: container.height - root.height,
  67. });
  68. } else if (offsetTop >= root.top) {
  69. this.setDataAfterDiff({
  70. fixed: true,
  71. height: root.height,
  72. transform: 0,
  73. });
  74. } else {
  75. this.setDataAfterDiff({ fixed: false, transform: 0 });
  76. }
  77. });
  78. return;
  79. }
  80. getRect(this, ROOT_ELEMENT).then((root) => {
  81. if (offsetTop >= root.top) {
  82. this.setDataAfterDiff({ fixed: true, height: root.height });
  83. this.transform = 0;
  84. } else {
  85. this.setDataAfterDiff({ fixed: false });
  86. }
  87. });
  88. },
  89. setDataAfterDiff(data) {
  90. wx.nextTick(() => {
  91. const diff = Object.keys(data).reduce((prev, key) => {
  92. if (data[key] !== this.data[key]) {
  93. prev[key] = data[key];
  94. }
  95. return prev;
  96. }, {});
  97. if (Object.keys(diff).length > 0) {
  98. this.setData(diff);
  99. }
  100. this.$emit('scroll', {
  101. scrollTop: this.scrollTop,
  102. isFixed: data.fixed || this.data.fixed,
  103. });
  104. });
  105. },
  106. getContainerRect() {
  107. const nodesRef = this.data.container();
  108. return new Promise((resolve) =>
  109. nodesRef.boundingClientRect(resolve).exec()
  110. );
  111. },
  112. },
  113. });