tui-sticky-wxs.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <view class="tui-sticky-class" :change:prop="parse.stickyChange" :prop="scrollTop" :data-top="top" :data-height="height"
  3. :data-stickytop="stickyTop" :data-container="container" :data-isNativeHeader="isNativeHeader" :data-index="index">
  4. <!--sticky 容器-->
  5. <view class="tui-sticky-seat" :style="{ height: stickyHeight, backgroundColor: backgroundColor }"></view>
  6. <view class="tui-sticky-bar">
  7. <slot name="header"></slot>
  8. </view>
  9. <!--sticky 容器-->
  10. <!--内容-->
  11. <slot name="content"></slot>
  12. </view>
  13. </template>
  14. <script src="./tui-sticky.wxs" module="parse" lang="wxs"></script>
  15. <script>
  16. export default {
  17. name: 'tuiStickyWxs',
  18. emits: ['prop', 'change'],
  19. props: {
  20. scrollTop: {
  21. type: [Number, String],
  22. value: 0
  23. },
  24. //吸顶时与顶部的距离,单位px
  25. stickyTop: {
  26. type: [Number, String],
  27. // #ifndef H5
  28. default: 0,
  29. // #endif
  30. // #ifdef H5
  31. default: 44
  32. // #endif
  33. },
  34. //是否指定容器,即内容放置插槽content内
  35. container: {
  36. type: Boolean,
  37. default: false
  38. },
  39. //是否是原生自带header
  40. isNativeHeader: {
  41. type: Boolean,
  42. default: true
  43. },
  44. //吸顶容器 高度 rpx
  45. stickyHeight: {
  46. type: String,
  47. default: 'auto'
  48. },
  49. //占位容器背景颜色
  50. backgroundColor: {
  51. type: String,
  52. default: 'transparent'
  53. },
  54. //是否重新计算[有异步加载时使用,设置大于0的数]
  55. recalc: {
  56. type: Number,
  57. default: 0
  58. },
  59. //列表中的索引值
  60. index: {
  61. type: [Number, String],
  62. default: 0
  63. }
  64. },
  65. watch: {
  66. recalc(newValue, oldValue) {
  67. this.updateScrollChange(() => {
  68. //更新prop scrollTop值(+0.1即可),触发change事件
  69. this.$emit("prop",{})
  70. });
  71. }
  72. },
  73. mounted() {
  74. setTimeout(() => {
  75. this.updateScrollChange();
  76. }, 20);
  77. },
  78. data() {
  79. return {
  80. timer: null,
  81. top: 0,
  82. height: 0
  83. };
  84. },
  85. methods: {
  86. updateScrollChange(callback) {
  87. if (this.timer) {
  88. clearTimeout(this.timer);
  89. this.timer = null;
  90. }
  91. this.timer = setTimeout(() => {
  92. const className = '.tui-sticky-class';
  93. const query = uni.createSelectorQuery().in(this);
  94. query
  95. .select(className)
  96. .boundingClientRect(res => {
  97. if (res) {
  98. this.top = res.top + (this.scrollTop || 0);
  99. this.height = res.height;
  100. callback && callback();
  101. this.$emit('change', {
  102. index: Number(this.index),
  103. top: this.top
  104. });
  105. }
  106. })
  107. .exec();
  108. }, 0);
  109. }
  110. }
  111. };
  112. </script>
  113. <style scoped>
  114. .tui-sticky-fixed {
  115. width: 100%;
  116. position: fixed;
  117. left: 0;
  118. z-index: 998;
  119. }
  120. .tui-sticky-seat {
  121. display: none;
  122. }
  123. </style>