z-paging-swiper.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <!-- z-paging -->
  2. <!-- github地址:https://github.com/SmileZXLee/uni-z-paging -->
  3. <!-- dcloud地址:https://ext.dcloud.net.cn/plugin?id=3935 -->
  4. <!-- 反馈QQ群:790460711 -->
  5. <!-- 滑动切换选项卡swiper,此组件支持easycom规范,可以在项目中直接引用 -->
  6. <template>
  7. <view :class="fixed?'zp-swiper-container zp-swiper-container-fixed':'zp-swiper-container'" :style="[finalSwiperStyle]">
  8. <!-- #ifndef APP-PLUS -->
  9. <view v-if="cssSafeAreaInsetBottom===-1" class="zp-safe-area-inset-bottom"></view>
  10. <!-- #endif -->
  11. <slot v-if="$slots.top" name="top" />
  12. <view class="zp-swiper-super">
  13. <view class="zp-swiper">
  14. <slot />
  15. </view>
  16. </view>
  17. <slot v-if="$slots.bottom" name="bottom" />
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. name: "z-paging-swiper",
  23. data() {
  24. return {
  25. systemInfo: null,
  26. cssSafeAreaInsetBottom: -1
  27. };
  28. },
  29. props: {
  30. //是否使用fixed布局,默认为是
  31. fixed: {
  32. type: Boolean,
  33. default: true
  34. },
  35. //是否开启底部安全区域适配
  36. safeAreaInsetBottom: {
  37. type: Boolean,
  38. default: false
  39. },
  40. //z-paging-swiper样式
  41. swiperStyle: {
  42. type: Object,
  43. default: function() {
  44. return {};
  45. },
  46. }
  47. },
  48. mounted() {
  49. this.$nextTick(() => {
  50. this.systemInfo = uni.getSystemInfoSync();
  51. })
  52. // #ifndef APP-PLUS
  53. this._getCssSafeAreaInsetBottom();
  54. // #endif
  55. },
  56. computed: {
  57. finalSwiperStyle() {
  58. let swiperStyle = this.swiperStyle;
  59. if (!this.systemInfo) return swiperStyle;
  60. const windowTop = this.systemInfo.windowTop;
  61. const windowBottom = this.systemInfo.windowBottom;
  62. if (this.fixed) {
  63. if (windowTop && !swiperStyle.top) {
  64. swiperStyle.top = windowTop + 'px';
  65. }
  66. if (!swiperStyle.bottom) {
  67. let bottom = windowBottom ? windowBottom : 0;
  68. if (this.safeAreaInsetBottom) {
  69. bottom += this.safeAreaBottom;
  70. }
  71. if(bottom > 0){
  72. swiperStyle.bottom = bottom + 'px';
  73. }
  74. }
  75. }
  76. return swiperStyle;
  77. },
  78. safeAreaBottom() {
  79. if(!this.systemInfo){
  80. return 0;
  81. }
  82. let safeAreaBottom = 0;
  83. // #ifdef APP-PLUS
  84. safeAreaBottom = this.systemInfo.safeAreaInsets.bottom || 0;
  85. // #endif
  86. // #ifndef APP-PLUS
  87. safeAreaBottom = this.cssSafeAreaInsetBottom === -1 ? 0 : this.cssSafeAreaInsetBottom;
  88. // #endif
  89. return safeAreaBottom;
  90. }
  91. },
  92. methods: {
  93. //通过获取css设置的底部安全区域占位view高度设置bottom距离
  94. _getCssSafeAreaInsetBottom(){
  95. const query = uni.createSelectorQuery().in(this);
  96. query.select('.zp-safe-area-inset-bottom').boundingClientRect(res => {
  97. if (res) {
  98. this.cssSafeAreaInsetBottom = res.height;
  99. }
  100. }).exec();
  101. }
  102. }
  103. }
  104. </script>
  105. <style scoped>
  106. .zp-swiper-container {
  107. /* #ifndef APP-NVUE */
  108. display: flex;
  109. /* #endif */
  110. flex-direction: column;
  111. flex: 1;
  112. }
  113. .zp-swiper-container-fixed {
  114. position: fixed;
  115. /* #ifndef APP-NVUE */
  116. height: auto;
  117. width: auto;
  118. /* #endif */
  119. top: 0;
  120. left: 0;
  121. bottom: 0;
  122. right: 0;
  123. }
  124. .zp-safe-area-inset-bottom {
  125. position: absolute;
  126. /* #ifndef APP-PLUS */
  127. height: env(safe-area-inset-bottom);
  128. /* #endif */
  129. }
  130. .zp-swiper-super {
  131. flex: 1;
  132. position: relative;
  133. }
  134. .zp-swiper {
  135. /* #ifndef APP-NVUE */
  136. height: 100%;
  137. width: 100%;
  138. position: absolute;
  139. top: 0;
  140. left: 0;
  141. /* #endif */
  142. /* #ifdef APP-NVUE */
  143. flex: 1;
  144. /* #endif */
  145. }
  146. .zp-swiper-item {
  147. height: 100%;
  148. }
  149. </style>