index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <uni-shadow-root class="vant-sticky-index"><view class="custom-class van-sticky" :style="computed.containerStyle({ fixed, height, zIndex })">
  3. <view :class="utils.bem('sticky-wrap', { fixed })" :style="computed.wrapStyle({ fixed, offsetTop, transform, zIndex })">
  4. <slot></slot>
  5. </view>
  6. </view></uni-shadow-root>
  7. </template>
  8. <wxs src="../wxs/utils.wxs" module="utils"></wxs><wxs src="./index.wxs" module="computed"></wxs>
  9. <script>
  10. global['__wxRoute'] = 'vant/sticky/index'
  11. import { getRect } from '../common/utils';
  12. import { VantComponent } from '../common/component';
  13. import { isDef } from '../common/validator';
  14. import { pageScrollMixin } from '../mixins/page-scroll';
  15. const ROOT_ELEMENT = '.van-sticky';
  16. VantComponent({
  17. props: {
  18. zIndex: {
  19. type: Number,
  20. value: 99,
  21. },
  22. offsetTop: {
  23. type: Number,
  24. value: 0,
  25. observer: 'onScroll',
  26. },
  27. disabled: {
  28. type: Boolean,
  29. observer: 'onScroll',
  30. },
  31. container: {
  32. type: null,
  33. observer: 'onScroll',
  34. },
  35. scrollTop: {
  36. type: null,
  37. observer(val) {
  38. this.onScroll({ scrollTop: val });
  39. },
  40. },
  41. },
  42. mixins: [
  43. pageScrollMixin(function (event) {
  44. if (this.data.scrollTop != null) {
  45. return;
  46. }
  47. this.onScroll(event);
  48. }),
  49. ],
  50. data: {
  51. height: 0,
  52. fixed: false,
  53. transform: 0,
  54. },
  55. mounted() {
  56. this.onScroll();
  57. },
  58. methods: {
  59. onScroll({ scrollTop } = {}) {
  60. const { container, offsetTop, disabled } = this.data;
  61. if (disabled) {
  62. this.setDataAfterDiff({
  63. fixed: false,
  64. transform: 0,
  65. });
  66. return;
  67. }
  68. this.scrollTop = scrollTop || this.scrollTop;
  69. if (typeof container === 'function') {
  70. Promise.all([getRect(this, ROOT_ELEMENT), this.getContainerRect()])
  71. .then(([root, container]) => {
  72. if (offsetTop + root.height > container.height + container.top) {
  73. this.setDataAfterDiff({
  74. fixed: false,
  75. transform: container.height - root.height,
  76. });
  77. }
  78. else if (offsetTop >= root.top) {
  79. this.setDataAfterDiff({
  80. fixed: true,
  81. height: root.height,
  82. transform: 0,
  83. });
  84. }
  85. else {
  86. this.setDataAfterDiff({ fixed: false, transform: 0 });
  87. }
  88. })
  89. .catch(() => { });
  90. return;
  91. }
  92. getRect(this, ROOT_ELEMENT).then((root) => {
  93. if (!isDef(root) || (!root.width && !root.height)) {
  94. return;
  95. }
  96. if (offsetTop >= root.top) {
  97. this.setDataAfterDiff({ fixed: true, height: root.height });
  98. this.transform = 0;
  99. }
  100. else {
  101. this.setDataAfterDiff({ fixed: false });
  102. }
  103. });
  104. },
  105. setDataAfterDiff(data) {
  106. wx.nextTick(() => {
  107. const diff = Object.keys(data).reduce((prev, key) => {
  108. if (data[key] !== this.data[key]) {
  109. prev[key] = data[key];
  110. }
  111. return prev;
  112. }, {});
  113. if (Object.keys(diff).length > 0) {
  114. this.setData(diff);
  115. }
  116. this.$emit('scroll', {
  117. scrollTop: this.scrollTop,
  118. isFixed: data.fixed || this.data.fixed,
  119. });
  120. });
  121. },
  122. getContainerRect() {
  123. const nodesRef = this.data.container();
  124. if (!nodesRef) {
  125. return Promise.reject(new Error('not found container'));
  126. }
  127. return new Promise((resolve) => nodesRef.boundingClientRect(resolve).exec());
  128. },
  129. },
  130. });
  131. export default global['__wxComponents']['vant/sticky/index']
  132. </script>
  133. <style platform="mp-weixin">
  134. @import '../common/index.css';.van-sticky{position:relative}.van-sticky-wrap--fixed{left:0;position:fixed;right:0}
  135. </style>