tui-sticky.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view class="tui-sticky-class">
  3. <!--sticky 容器-->
  4. <view :style="{height: stickyHeight,backgroundColor: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. }, 20)
  89. },
  90. data() {
  91. return {
  92. timer: null,
  93. top: 0,
  94. height: 0,
  95. isFixed: false,
  96. initialize: 0 //重新初始化
  97. };
  98. },
  99. methods: {
  100. updateStickyChange() {
  101. const top = this.top;
  102. const height = this.height;
  103. const scrollTop = this.scrollTop
  104. let stickyTop = this.stickyTop
  105. // #ifdef H5
  106. if (this.isNativeHeader) {
  107. stickyTop = stickyTop - 44
  108. stickyTop = stickyTop < 0 ? 0 : stickyTop
  109. }
  110. // #endif
  111. if (this.container) {
  112. this.isFixed = (scrollTop + stickyTop >= top && scrollTop + stickyTop < top + height) ? true : false
  113. } else {
  114. this.isFixed = scrollTop + stickyTop >= top ? true : false
  115. }
  116. //是否吸顶
  117. this.$emit("sticky", {
  118. isFixed: this.isFixed,
  119. index: this.index
  120. })
  121. },
  122. updateScrollChange(callback) {
  123. if (this.timer) {
  124. clearTimeout(this.timer)
  125. this.timer = null
  126. }
  127. this.timer = setTimeout(() => {
  128. const className = '.tui-sticky-class';
  129. const query = uni.createSelectorQuery().in(this);
  130. query.select(className).boundingClientRect((res) => {
  131. if (res) {
  132. this.top = res.top + (this.scrollTop || 0);
  133. this.height = res.height;
  134. callback && callback();
  135. this.$emit("change", {
  136. index: Number(this.index),
  137. top: this.top
  138. })
  139. }
  140. }).exec()
  141. }, 0)
  142. }
  143. }
  144. }
  145. </script>
  146. <style scoped>
  147. .tui-sticky-fixed {
  148. width: 100%;
  149. position: fixed;
  150. left: 0;
  151. z-index: 888;
  152. }
  153. </style>