index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 { VantComponent } from '../common/component';
  12. import { pageScrollMixin } from '../mixins/page-scroll';
  13. const ROOT_ELEMENT = '.van-sticky';
  14. VantComponent({
  15. props: {
  16. zIndex: {
  17. type: Number,
  18. value: 99,
  19. },
  20. offsetTop: {
  21. type: Number,
  22. value: 0,
  23. observer: 'onScroll',
  24. },
  25. disabled: {
  26. type: Boolean,
  27. observer: 'onScroll',
  28. },
  29. container: {
  30. type: null,
  31. observer: 'onScroll',
  32. },
  33. scrollTop: {
  34. type: null,
  35. observer(val) {
  36. this.onScroll({ scrollTop: val });
  37. },
  38. },
  39. },
  40. mixins: [
  41. pageScrollMixin(function (event) {
  42. if (this.data.scrollTop != null) {
  43. return;
  44. }
  45. this.onScroll(event);
  46. }),
  47. ],
  48. data: {
  49. height: 0,
  50. fixed: false,
  51. transform: 0,
  52. },
  53. mounted() {
  54. this.onScroll();
  55. },
  56. methods: {
  57. onScroll(obj) {
  58. obj = obj||{}
  59. let scrollTop = obj.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([this.getRect(ROOT_ELEMENT), this.getContainerRect()]).then(
  71. ([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. } 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. );
  88. return;
  89. }
  90. this.getRect(ROOT_ELEMENT).then((root) => {
  91. if (offsetTop >= root.top) {
  92. this.setDataAfterDiff({ fixed: true, height: root.height });
  93. this.transform = 0;
  94. } else {
  95. this.setDataAfterDiff({ fixed: false });
  96. }
  97. });
  98. },
  99. setDataAfterDiff(data) {
  100. wx.nextTick(() => {
  101. const diff = Object.keys(data).reduce((prev, key) => {
  102. if (data[key] !== this.data[key]) {
  103. prev[key] = data[key];
  104. }
  105. return prev;
  106. }, {});
  107. this.setData(diff);
  108. this.$emit('scroll', {
  109. scrollTop: this.scrollTop,
  110. isFixed: data.fixed || this.data.fixed,
  111. });
  112. });
  113. },
  114. getContainerRect() {
  115. const nodesRef = this.data.container();
  116. return new Promise((resolve) =>
  117. nodesRef.boundingClientRect(resolve).exec()
  118. );
  119. },
  120. },
  121. });
  122. export default global['__wxComponents']['vant/sticky/index']
  123. </script>
  124. <style platform="mp-weixin">
  125. @import '../common/index.css';.van-sticky{position:relative}.van-sticky-wrap--fixed{position:fixed;right:0;left:0}
  126. </style>