u-sticky.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <view class="">
  3. <view class="u-sticky-wrap" :class="[elClass]" :style="{
  4. height: fixed ? height + 'px' : 'auto',
  5. backgroundColor: bgColor
  6. }">
  7. <view class="u-sticky" :style="{
  8. position: fixed ? 'fixed' : 'static',
  9. top: stickyTop + 'px',
  10. left: left + 'px',
  11. width: width == 'auto' ? 'auto' : width + 'px',
  12. zIndex: uZIndex,
  13. backgroundColor: fixed ? bgColors : ''
  14. }">
  15. <slot></slot>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. /**
  22. * sticky 吸顶
  23. * @description 该组件与CSS中position: sticky属性实现的效果一致,当组件达到预设的到顶部距离时, 就会固定在指定位置,组件位置大于预设的顶部距离时,会重新按照正常的布局排列。
  24. * @tutorial https://www.uviewui.com/components/sticky.html
  25. * @property {String Number} offset-top 吸顶时与顶部的距离,单位rpx(默认0)
  26. * @property {String Number} index 自定义标识,用于区分是哪一个组件
  27. * @property {Boolean} enable 是否开启吸顶功能(默认true)
  28. * @property {String} bg-color 组件背景颜色(默认#ffffff)
  29. * @property {String Number} z-index 吸顶时的z-index值(默认970)
  30. * @property {String Number} h5-nav-height 导航栏高度,自定义导航栏时(无导航栏时需设置为0),需要传入此值,单位px(默认44)
  31. * @event {Function} fixed 组件吸顶时触发
  32. * @event {Function} unfixed 组件取消吸顶时触发
  33. * @example <u-sticky offset-top="200"><view>塞下秋来风景异,衡阳雁去无留意</view></u-sticky>
  34. */
  35. export default {
  36. name: "u-sticky",
  37. props: {
  38. // 吸顶容器到顶部某个距离的时候,进行吸顶,在H5平台,NavigationBar为44px
  39. offsetTop: {
  40. type: [Number, String],
  41. default: 0
  42. },
  43. //列表中的索引值
  44. index: {
  45. type: [Number, String],
  46. default: ''
  47. },
  48. // 是否开启吸顶功能
  49. enable: {
  50. type: Boolean,
  51. default: true
  52. },
  53. // h5顶部导航栏的高度
  54. h5NavHeight: {
  55. type: [Number, String],
  56. default: 44
  57. },
  58. bgColors:{
  59. type: String,
  60. default: 'none'
  61. },
  62. // 吸顶区域的背景颜色
  63. bgColor: {
  64. type: String,
  65. default: '#ffffff'
  66. },
  67. // z-index值
  68. zIndex: {
  69. type: [Number, String],
  70. default: ''
  71. }
  72. },
  73. data() {
  74. return {
  75. fixed: false,
  76. height: 'auto',
  77. stickyTop: 0,
  78. elClass: this.$u.guid(),
  79. left: 0,
  80. width: 'auto',
  81. };
  82. },
  83. watch: {
  84. offsetTop(val) {
  85. this.initObserver();
  86. },
  87. enable(val) {
  88. if (val == false) {
  89. this.fixed = false;
  90. this.disconnectObserver('contentObserver');
  91. } else {
  92. this.initObserver();
  93. }
  94. }
  95. },
  96. computed: {
  97. uZIndex() {
  98. return this.zIndex ? this.zIndex : this.$u.zIndex.sticky;
  99. }
  100. },
  101. mounted() {
  102. this.initObserver();
  103. },
  104. methods: {
  105. initObserver() {
  106. if (!this.enable) return;
  107. // #ifdef H5
  108. this.stickyTop = this.offsetTop != 0 ? uni.upx2px(this.offsetTop) + this.h5NavHeight : this.h5NavHeight;
  109. // #endif
  110. // #ifndef H5
  111. this.stickyTop = this.offsetTop != 0 ? uni.upx2px(this.offsetTop) : 0;
  112. // #endif
  113. this.disconnectObserver('contentObserver');
  114. this.$uGetRect('.' + this.elClass).then((res) => {
  115. this.height = res.height;
  116. this.left = res.left;
  117. this.width = res.width;
  118. this.$nextTick(() => {
  119. this.observeContent();
  120. });
  121. });
  122. },
  123. observeContent() {
  124. this.disconnectObserver('contentObserver');
  125. const contentObserver = this.createIntersectionObserver({
  126. thresholds: [0.95, 0.98, 1]
  127. });
  128. contentObserver.relativeToViewport({
  129. top: -this.stickyTop
  130. });
  131. contentObserver.observe('.' + this.elClass, res => {
  132. if (!this.enable) return;
  133. this.setFixed(res.boundingClientRect.top);
  134. });
  135. this.contentObserver = contentObserver;
  136. },
  137. setFixed(top) {
  138. const fixed = top < this.stickyTop;
  139. if (fixed) this.$emit('fixed', this.index);
  140. else if(this.fixed) this.$emit('unfixed', this.index);
  141. this.fixed = fixed;
  142. },
  143. disconnectObserver(observerName) {
  144. const observer = this[observerName];
  145. observer && observer.disconnect();
  146. },
  147. },
  148. beforeDestroy() {
  149. this.disconnectObserver('contentObserver');
  150. }
  151. };
  152. </script>
  153. <style scoped lang="scss">
  154. @import "../../libs/css/style.components.scss";
  155. .u-sticky {
  156. z-index: 9999999999;
  157. }
  158. </style>