index.js 3.1 KB

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