index.js 3.2 KB

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