tui-sticky.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <view class="tui-sticky-class" :id="tui_Id">
  3. <!--sticky 容器-->
  4. <view :style="{height: stickyHeight,background:backgroundColor}" v-if="isFixed"></view>
  5. <view :class="{'tui-sticky-fixed':isFixed}" :style="{top:isFixed?stickyTop+'px':'auto'}">
  6. <slot name="header"></slot>
  7. </view>
  8. <!--sticky 容器-->
  9. <!--内容-->
  10. <slot name="content"></slot>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. name: "tuiSticky",
  16. emits: ['sticky', 'change'],
  17. props: {
  18. scrollTop: {
  19. type: Number
  20. },
  21. //吸顶时与顶部的距离,单位px
  22. stickyTop: {
  23. type: [Number, String]
  24. // #ifndef H5
  25. ,
  26. default: 0
  27. // #endif
  28. // #ifdef H5
  29. ,
  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. scrollTop(newValue, oldValue) {
  66. if (this.initialize != 0) {
  67. this.updateScrollChange(() => {
  68. this.updateStickyChange();
  69. this.initialize = 0
  70. });
  71. } else {
  72. this.updateStickyChange();
  73. }
  74. },
  75. recalc(newValue, oldValue) {
  76. this.updateScrollChange(() => {
  77. this.updateStickyChange();
  78. this.initialize = 0;
  79. });
  80. }
  81. },
  82. created() {
  83. this.initialize = this.recalc
  84. },
  85. mounted() {
  86. setTimeout(() => {
  87. this.updateScrollChange();
  88. }, 50)
  89. },
  90. data() {
  91. const Id = `tui_${Math.ceil(Math.random() * 10e5).toString(36)}`
  92. return {
  93. timer: null,
  94. top: 0,
  95. height: 0,
  96. isFixed: false,
  97. initialize: 0, //重新初始化
  98. tui_Id: Id
  99. };
  100. },
  101. methods: {
  102. updateStickyChange() {
  103. const top = this.top;
  104. const height = this.height;
  105. const scrollTop = this.scrollTop
  106. let stickyTop = this.stickyTop
  107. // #ifdef H5
  108. if (this.isNativeHeader) {
  109. stickyTop = stickyTop - 44
  110. stickyTop = stickyTop < 0 ? 0 : stickyTop
  111. }
  112. // #endif
  113. if (this.container) {
  114. this.isFixed = (scrollTop + stickyTop >= top && scrollTop + stickyTop < top + height) ? true : false
  115. } else {
  116. this.isFixed = scrollTop + stickyTop >= top ? true : false
  117. }
  118. //是否吸顶
  119. this.$emit("sticky", {
  120. isFixed: this.isFixed,
  121. index: this.index
  122. })
  123. },
  124. updateScrollChange(callback) {
  125. if (this.timer) {
  126. clearTimeout(this.timer)
  127. this.timer = null
  128. }
  129. this.timer = setTimeout(() => {
  130. const selectId = `#${this.tui_Id}`;
  131. uni.createSelectorQuery()
  132. // #ifndef MP-ALIPAY
  133. .in(this)
  134. // #endif
  135. .select(selectId)
  136. .fields({
  137. size: true,
  138. rect: true
  139. }, res => {
  140. if (res) {
  141. this.top = res.top + (this.scrollTop || 0);
  142. this.height = res.height;
  143. callback && callback();
  144. this.$emit("change", {
  145. index: Number(this.index),
  146. top: this.top
  147. })
  148. }
  149. }).exec()
  150. }, 0)
  151. }
  152. }
  153. }
  154. </script>
  155. <style scoped>
  156. .tui-sticky-fixed {
  157. width: 100%;
  158. position: fixed;
  159. left: 0;
  160. z-index: 888;
  161. }
  162. </style>