tui-sticky-wxs.vue 2.6 KB

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