index.js 3.3 KB

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