index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. (0, 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. (0, 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, scrollTop = _b.scrollTop;
  54. var _c = this.data, container = _c.container, offsetTop = _c.offsetTop, disabled = _c.disabled;
  55. if (disabled) {
  56. this.setDataAfterDiff({
  57. fixed: false,
  58. transform: 0,
  59. });
  60. return;
  61. }
  62. this.scrollTop = scrollTop || this.scrollTop;
  63. if (typeof container === 'function') {
  64. Promise.all([(0, utils_1.getRect)(this, ROOT_ELEMENT), this.getContainerRect()])
  65. .then(function (_a) {
  66. var root = _a[0], container = _a[1];
  67. if (offsetTop + root.height > container.height + container.top) {
  68. _this.setDataAfterDiff({
  69. fixed: false,
  70. transform: container.height - root.height,
  71. });
  72. }
  73. else if (offsetTop >= root.top) {
  74. _this.setDataAfterDiff({
  75. fixed: true,
  76. height: root.height,
  77. transform: 0,
  78. });
  79. }
  80. else {
  81. _this.setDataAfterDiff({ fixed: false, transform: 0 });
  82. }
  83. })
  84. .catch(function () { });
  85. return;
  86. }
  87. (0, utils_1.getRect)(this, ROOT_ELEMENT).then(function (root) {
  88. if (!(0, validator_1.isDef)(root) || (!root.width && !root.height)) {
  89. return;
  90. }
  91. if (offsetTop >= root.top) {
  92. _this.setDataAfterDiff({ fixed: true, height: root.height });
  93. _this.transform = 0;
  94. }
  95. else {
  96. _this.setDataAfterDiff({ fixed: false });
  97. }
  98. });
  99. },
  100. setDataAfterDiff: function (data) {
  101. var _this = this;
  102. wx.nextTick(function () {
  103. var diff = Object.keys(data).reduce(function (prev, key) {
  104. if (data[key] !== _this.data[key]) {
  105. prev[key] = data[key];
  106. }
  107. return prev;
  108. }, {});
  109. if (Object.keys(diff).length > 0) {
  110. _this.setData(diff);
  111. }
  112. _this.$emit('scroll', {
  113. scrollTop: _this.scrollTop,
  114. isFixed: data.fixed || _this.data.fixed,
  115. });
  116. });
  117. },
  118. getContainerRect: function () {
  119. var nodesRef = this.data.container();
  120. if (!nodesRef) {
  121. return Promise.reject(new Error('not found container'));
  122. }
  123. return new Promise(function (resolve) { return nodesRef.boundingClientRect(resolve).exec(); });
  124. },
  125. },
  126. });