index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var component_1 = require("../common/component");
  4. var ROOT_ELEMENT = '.van-sticky';
  5. component_1.VantComponent({
  6. props: {
  7. zIndex: {
  8. type: Number,
  9. value: 99
  10. },
  11. offsetTop: {
  12. type: Number,
  13. value: 0,
  14. observer: 'observeContent'
  15. },
  16. disabled: {
  17. type: Boolean,
  18. observer: function (value) {
  19. if (!this.mounted) {
  20. return;
  21. }
  22. value ? this.disconnectObserver() : this.initObserver();
  23. }
  24. },
  25. container: {
  26. type: null,
  27. observer: function (target) {
  28. if (typeof target !== 'function' || !this.data.height) {
  29. return;
  30. }
  31. this.observeContainer();
  32. this.updateFixed();
  33. }
  34. }
  35. },
  36. data: {
  37. height: 0,
  38. fixed: false
  39. },
  40. methods: {
  41. getContainerRect: function () {
  42. var nodesRef = this.data.container();
  43. return new Promise(function (resolve) {
  44. return nodesRef.boundingClientRect(resolve).exec();
  45. });
  46. },
  47. initObserver: function () {
  48. var _this = this;
  49. this.disconnectObserver();
  50. this.getRect(ROOT_ELEMENT).then(function (rect) {
  51. _this.setData({ height: rect.height });
  52. wx.nextTick(function () {
  53. _this.observeContent();
  54. _this.observeContainer();
  55. });
  56. });
  57. },
  58. updateFixed: function () {
  59. var _this = this;
  60. Promise.all([this.getRect(ROOT_ELEMENT), this.getContainerRect()]).then(function (_a) {
  61. var content = _a[0], container = _a[1];
  62. _this.setData({ height: content.height });
  63. _this.containerHeight = container.height;
  64. wx.nextTick(function () {
  65. _this.setFixed(content.top);
  66. });
  67. });
  68. },
  69. disconnectObserver: function (observerName) {
  70. if (observerName) {
  71. var observer = this[observerName];
  72. observer && observer.disconnect();
  73. }
  74. else {
  75. this.contentObserver && this.contentObserver.disconnect();
  76. this.containerObserver && this.containerObserver.disconnect();
  77. }
  78. },
  79. observeContent: function () {
  80. var _this = this;
  81. var offsetTop = this.data.offsetTop;
  82. this.disconnectObserver('contentObserver');
  83. var contentObserver = this.createIntersectionObserver({
  84. thresholds: [0.9, 1]
  85. });
  86. contentObserver.relativeToViewport({ top: -offsetTop });
  87. contentObserver.observe(ROOT_ELEMENT, function (res) {
  88. if (_this.data.disabled) {
  89. return;
  90. }
  91. _this.setFixed(res.boundingClientRect.top);
  92. });
  93. this.contentObserver = contentObserver;
  94. },
  95. observeContainer: function () {
  96. var _this = this;
  97. if (typeof this.data.container !== 'function') {
  98. return;
  99. }
  100. var height = this.data.height;
  101. this.getContainerRect().then(function (rect) {
  102. _this.containerHeight = rect.height;
  103. _this.disconnectObserver('containerObserver');
  104. var containerObserver = _this.createIntersectionObserver({
  105. thresholds: [0.9, 1]
  106. });
  107. _this.containerObserver = containerObserver;
  108. containerObserver.relativeToViewport({
  109. top: _this.containerHeight - height
  110. });
  111. containerObserver.observe(ROOT_ELEMENT, function (res) {
  112. if (_this.data.disabled) {
  113. return;
  114. }
  115. _this.setFixed(res.boundingClientRect.top);
  116. });
  117. });
  118. },
  119. setFixed: function (top) {
  120. var _a = this.data, offsetTop = _a.offsetTop, height = _a.height;
  121. var containerHeight = this.containerHeight;
  122. var fixed = containerHeight && height
  123. ? top >= height - containerHeight && top < offsetTop
  124. : top < offsetTop;
  125. this.$emit('scroll', {
  126. scrollTop: top,
  127. isFixed: fixed
  128. });
  129. this.setData({ fixed: fixed });
  130. }
  131. },
  132. mounted: function () {
  133. this.mounted = true;
  134. if (!this.data.disabled) {
  135. this.initObserver();
  136. }
  137. },
  138. destroyed: function () {
  139. this.disconnectObserver();
  140. }
  141. });