index.js 4.1 KB

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