index.vue 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <uni-shadow-root class="vant-dist-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/dist/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([
  71. getRect(this, ROOT_ELEMENT),
  72. this.getContainerRect(),
  73. ]).then(([root, container]) => {
  74. if (offsetTop + root.height > container.height + container.top) {
  75. this.setDataAfterDiff({
  76. fixed: false,
  77. transform: container.height - root.height,
  78. });
  79. }
  80. else if (offsetTop >= root.top) {
  81. this.setDataAfterDiff({
  82. fixed: true,
  83. height: root.height,
  84. transform: 0,
  85. });
  86. }
  87. else {
  88. this.setDataAfterDiff({ fixed: false, transform: 0 });
  89. }
  90. });
  91. return;
  92. }
  93. getRect(this, ROOT_ELEMENT).then((root) => {
  94. if (!isDef(root)) {
  95. return;
  96. }
  97. if (offsetTop >= root.top) {
  98. this.setDataAfterDiff({ fixed: true, height: root.height });
  99. this.transform = 0;
  100. }
  101. else {
  102. this.setDataAfterDiff({ fixed: false });
  103. }
  104. });
  105. },
  106. setDataAfterDiff(data) {
  107. wx.nextTick(() => {
  108. const diff = Object.keys(data).reduce((prev, key) => {
  109. if (data[key] !== this.data[key]) {
  110. prev[key] = data[key];
  111. }
  112. return prev;
  113. }, {});
  114. if (Object.keys(diff).length > 0) {
  115. this.setData(diff);
  116. }
  117. this.$emit('scroll', {
  118. scrollTop: this.scrollTop,
  119. isFixed: data.fixed || this.data.fixed,
  120. });
  121. });
  122. },
  123. getContainerRect() {
  124. const nodesRef = this.data.container();
  125. return new Promise((resolve) => nodesRef.boundingClientRect(resolve).exec());
  126. },
  127. },
  128. });
  129. export default global['__wxComponents']['vant/dist/sticky/index']
  130. </script>
  131. <style platform="mp-weixin">
  132. @import '../common/index.css';.van-sticky{position:relative}.van-sticky-wrap--fixed{position:fixed;right:0;left:0}
  133. </style>