综合办公系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uv-sticky.vue 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <view
  3. class="uv-sticky"
  4. :id="elId"
  5. :style="[style]"
  6. >
  7. <view
  8. :style="[stickyContent]"
  9. class="uv-sticky__content"
  10. >
  11. <slot />
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  17. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  18. import props from './props.js';;
  19. /**
  20. * sticky 吸顶
  21. * @description 该组件与CSS中position: sticky属性实现的效果一致,当组件达到预设的到顶部距离时, 就会固定在指定位置,组件位置大于预设的顶部距离时,会重新按照正常的布局排列。
  22. * @tutorial https://www.uvui.cn/components/sticky.html
  23. * @property {String | Number} offsetTop 吸顶时与顶部的距离,单位px(默认 0 )
  24. * @property {String | Number} customNavHeight 自定义导航栏的高度 (h5 默认44 其他默认 0 )
  25. * @property {Boolean} disabled 是否开启吸顶功能 (默认 false )
  26. * @property {String} bgColor 组件背景颜色(默认 '#ffffff' )
  27. * @property {String | Number} zIndex 吸顶时的z-index值
  28. * @property {String | Number} index 自定义标识,用于区分是哪一个组件
  29. * @property {Object} customStyle 组件的样式,对象形式
  30. * @example <uv-sticky offsetTop="200"><view>塞下秋来风景异,衡阳雁去无留意</view></uv-sticky>
  31. */
  32. export default {
  33. name: 'uv-sticky',
  34. mixins: [mpMixin, mixin, props],
  35. data() {
  36. return {
  37. cssSticky: false, // 是否使用css的sticky实现
  38. stickyTop: 0, // 吸顶的top值,因为可能受自定义导航栏影响,最终的吸顶值非offsetTop值
  39. elId: '',
  40. left: 0, // js模式时,吸顶的内容因为处于postition: fixed模式,为了和原来保持一致的样式,需要记录并重新设置它的left,height,width属性
  41. width: 'auto',
  42. height: 'auto',
  43. fixed: false, // js模式时,是否处于吸顶模式
  44. }
  45. },
  46. computed: {
  47. style() {
  48. const style = {}
  49. if(!this.disabled) {
  50. if (this.cssSticky) {
  51. style.position = 'sticky'
  52. style.zIndex = this.uZindex
  53. style.top = this.$uv.addUnit(this.stickyTop)
  54. } else {
  55. style.height = this.fixed ? this.height + 'px' : 'auto'
  56. }
  57. } else {
  58. // 无需吸顶时,设置会默认的relative(nvue)和非nvue的static静态模式即可
  59. // #ifdef APP-NVUE
  60. style.position = 'relative'
  61. // #endif
  62. // #ifndef APP-NVUE
  63. style.position = 'static'
  64. // #endif
  65. }
  66. style.backgroundColor = this.bgColor
  67. return this.$uv.deepMerge(style, this.$uv.addStyle(this.customStyle))
  68. },
  69. // 吸顶内容的样式
  70. stickyContent() {
  71. const style = {}
  72. if (!this.cssSticky) {
  73. style.position = this.fixed ? 'fixed' : 'static'
  74. style.top = this.stickyTop + 'px'
  75. style.left = this.left + 'px'
  76. style.width = this.width == 'auto' ? 'auto' : this.width + 'px'
  77. style.zIndex = this.uZindex
  78. }
  79. return style
  80. },
  81. uZindex() {
  82. return this.zIndex ? this.zIndex : 970
  83. }
  84. },
  85. created() {
  86. this.elId = this.$uv.guid();
  87. },
  88. mounted() {
  89. this.init()
  90. },
  91. methods: {
  92. init() {
  93. this.getStickyTop()
  94. // 判断使用的模式
  95. this.checkSupportCssSticky()
  96. // 如果不支持css sticky,则使用js方案,此方案性能比不上css方案
  97. if (!this.cssSticky) {
  98. !this.disabled && this.initObserveContent()
  99. }
  100. },
  101. initObserveContent() {
  102. // 获取吸顶内容的高度,用于在js吸顶模式时,给父元素一个填充高度,防止"塌陷"
  103. this.$uvGetRect('#' + this.elId).then((res) => {
  104. this.height = res.height
  105. this.left = res.left
  106. this.width = res.width
  107. this.$nextTick(() => {
  108. this.observeContent()
  109. })
  110. })
  111. },
  112. observeContent() {
  113. // 先断掉之前的观察
  114. this.disconnectObserver('contentObserver')
  115. const contentObserver = uni.createIntersectionObserver({
  116. // 检测的区间范围
  117. thresholds: [0.95, 0.98, 1]
  118. })
  119. // 到屏幕顶部的高度时触发
  120. contentObserver.relativeToViewport({
  121. top: -this.stickyTop
  122. })
  123. // 绑定观察的元素
  124. contentObserver.observe(`#${this.elId}`, res => {
  125. this.setFixed(res.boundingClientRect.top)
  126. })
  127. this.contentObserver = contentObserver
  128. },
  129. setFixed(top) {
  130. // 判断是否出于吸顶条件范围
  131. const fixed = top <= this.stickyTop
  132. this.fixed = fixed
  133. },
  134. disconnectObserver(observerName) {
  135. // 断掉观察,释放资源
  136. const observer = this[observerName]
  137. observer && observer.disconnect()
  138. },
  139. getStickyTop() {
  140. this.stickyTop = this.$uv.getPx(this.offsetTop) + this.$uv.getPx(this.customNavHeight)
  141. },
  142. async checkSupportCssSticky() {
  143. // #ifdef H5
  144. // H5,一般都是现代浏览器,是支持css sticky的,这里使用创建元素嗅探的形式判断
  145. if (this.checkCssStickyForH5()) {
  146. this.cssSticky = true
  147. }
  148. // #endif
  149. // 如果安卓版本高于8.0,依然认为是支持css sticky的(因为安卓7在某些机型,可能不支持sticky)
  150. if (this.$uv.os() === 'android' && Number(this.$uv.sys().system) > 8) {
  151. this.cssSticky = true
  152. }
  153. // APP-Vue和微信平台,通过computedStyle判断是否支持css sticky
  154. // #ifdef APP-VUE || MP-WEIXIN
  155. this.cssSticky = await this.checkComputedStyle()
  156. // #endif
  157. // ios上,从ios6开始,都是支持css sticky的
  158. if (this.$uv.os() === 'ios') {
  159. this.cssSticky = true
  160. }
  161. // nvue,是支持css sticky的
  162. // #ifdef APP-NVUE
  163. this.cssSticky = true
  164. // #endif
  165. },
  166. // 在APP和微信小程序上,通过uni.createSelectorQuery可以判断是否支持css sticky
  167. checkComputedStyle() {
  168. // 方法内进行判断,避免在其他平台生成无用代码
  169. // #ifdef APP-VUE || MP-WEIXIN
  170. return new Promise(resolve => {
  171. uni.createSelectorQuery().in(this).select('.uv-sticky').fields({
  172. computedStyle: ["position"]
  173. }).exec(e => {
  174. resolve('sticky' === e[0].position)
  175. })
  176. })
  177. // #endif
  178. },
  179. // H5通过创建元素的形式嗅探是否支持css sticky
  180. // 判断浏览器是否支持sticky属性
  181. checkCssStickyForH5() {
  182. // 方法内进行判断,避免在其他平台生成无用代码
  183. // #ifdef H5
  184. const vendorList = ['', '-webkit-', '-ms-', '-moz-', '-o-'],
  185. vendorListLength = vendorList.length,
  186. stickyElement = document.createElement('div')
  187. for (let i = 0; i < vendorListLength; i++) {
  188. stickyElement.style.position = vendorList[i] + 'sticky'
  189. if (stickyElement.style.position !== '') {
  190. return true
  191. }
  192. }
  193. return false;
  194. // #endif
  195. }
  196. },
  197. // #ifdef VUE2
  198. beforeDestroy() {
  199. this.disconnectObserver('contentObserver')
  200. },
  201. // #endif
  202. // #ifdef VUE3
  203. unmounted() {
  204. this.disconnectObserver('contentObserver')
  205. }
  206. // #endif
  207. }
  208. </script>
  209. <style lang="scss" scoped>
  210. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  211. .uv-sticky {
  212. /* #ifdef APP-VUE || MP-WEIXIN */
  213. // 此处默认写sticky属性,是为了给微信和APP通过uni.createSelectorQuery查询是否支持css sticky使用
  214. position: sticky;
  215. /* #endif */
  216. }
  217. </style>