z-paging-swiper.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 v-if="$slots.left" :class="{'zp-swiper-left':true,'zp-absoulte':isOldWebView}">
  14. <slot name="left" />
  15. </view>
  16. <view :class="{'zp-swiper':true,'zp-absoulte':isOldWebView}" :style="[swiperContentStyle]">
  17. <slot />
  18. </view>
  19. <view v-if="$slots.right" :class="{'zp-swiper-right':true,'zp-absoulte zp-right':isOldWebView}">
  20. <slot name="right" />
  21. </view>
  22. </view>
  23. <slot v-if="$slots.bottom" name="bottom" />
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. name: "z-paging-swiper",
  29. data() {
  30. return {
  31. systemInfo: null,
  32. cssSafeAreaInsetBottom: -1,
  33. swiperContentStyle: {}
  34. };
  35. },
  36. props: {
  37. //是否使用fixed布局,默认为是
  38. fixed: {
  39. type: Boolean,
  40. default: true
  41. },
  42. //是否开启底部安全区域适配
  43. safeAreaInsetBottom: {
  44. type: Boolean,
  45. default: false
  46. },
  47. //z-paging-swiper样式
  48. swiperStyle: {
  49. type: Object,
  50. default: function() {
  51. return {};
  52. },
  53. }
  54. },
  55. mounted() {
  56. this.$nextTick(() => {
  57. this.systemInfo = uni.getSystemInfoSync();
  58. })
  59. // #ifndef APP-PLUS
  60. this._getCssSafeAreaInsetBottom();
  61. // #endif
  62. this._updateLeftAndRightWidth();
  63. this.swiperContentStyle = {'flex': '1'};
  64. // #ifndef APP-NVUE
  65. this.swiperContentStyle = {width: '100%',height: '100%'};
  66. // #endif
  67. },
  68. computed: {
  69. finalSwiperStyle() {
  70. const swiperStyle = this.swiperStyle;
  71. if (!this.systemInfo) return swiperStyle;
  72. let windowTop = this.systemInfo.windowTop;
  73. //暂时修复vue3中隐藏系统导航栏后windowTop获取不正确的问题,具体bug详见https://ask.dcloud.net.cn/question/141634
  74. //感谢litangyu!!https://github.com/SmileZXLee/uni-z-paging/issues/25
  75. // #ifdef VUE3 && H5
  76. const pageHeadNode = document.getElementsByTagName("uni-page-head");
  77. if (!pageHeadNode.length) windowTop = 0;
  78. // #endif
  79. const windowBottom = this.systemInfo.windowBottom;
  80. if (this.fixed) {
  81. if (windowTop && !swiperStyle.top) {
  82. swiperStyle.top = windowTop + 'px';
  83. }
  84. if (!swiperStyle.bottom) {
  85. let bottom = windowBottom ? windowBottom : 0;
  86. if (this.safeAreaInsetBottom) {
  87. bottom += this.safeAreaBottom;
  88. }
  89. if(bottom > 0){
  90. swiperStyle.bottom = bottom + 'px';
  91. }
  92. }
  93. }
  94. return swiperStyle;
  95. },
  96. safeAreaBottom() {
  97. if (!this.systemInfo) return 0;
  98. let safeAreaBottom = 0;
  99. // #ifdef APP-PLUS
  100. safeAreaBottom = this.systemInfo.safeAreaInsets.bottom || 0;
  101. // #endif
  102. // #ifndef APP-PLUS
  103. safeAreaBottom = this.cssSafeAreaInsetBottom === -1 ? 0 : this.cssSafeAreaInsetBottom;
  104. // #endif
  105. return safeAreaBottom;
  106. },
  107. isOldWebView() {
  108. // #ifndef APP-NVUE || MP-KUAISHOU
  109. try {
  110. const systemInfos = uni.getSystemInfoSync().system.split(' ');
  111. const deviceType = systemInfos[0];
  112. const version = parseInt(systemInfos[1].slice(0,1));
  113. if ((deviceType === 'iOS' && version <= 10) || (deviceType === 'Android' && version <= 6)) {
  114. return true;
  115. }
  116. } catch(e){
  117. return false;
  118. }
  119. // #endif
  120. return false;
  121. }
  122. },
  123. methods: {
  124. //更新slot="left"和slot="right"宽度,当slot="left"或slot="right"宽度动态改变时调用
  125. updateLeftAndRightWidth() {
  126. this.$nextTick(() => {
  127. this._updateLeftAndRightWidth();
  128. })
  129. },
  130. //通过获取css设置的底部安全区域占位view高度设置bottom距离
  131. _getCssSafeAreaInsetBottom() {
  132. const query = uni.createSelectorQuery().in(this);
  133. query.select('.zp-safe-area-inset-bottom').boundingClientRect(res => {
  134. if (res) {
  135. this.cssSafeAreaInsetBottom = res.height;
  136. }
  137. }).exec();
  138. },
  139. //获取slot="left"和slot="right"宽度并且更新布局
  140. _updateLeftAndRightWidth() {
  141. if (!this.isOldWebView) return;
  142. this.$nextTick(() => {
  143. let delayTime = 0;
  144. // #ifdef MP-BAIDU
  145. delayTime = 10;
  146. // #endif
  147. setTimeout(() => {
  148. ['left','right'].map(position => {
  149. const query = uni.createSelectorQuery().in(this);
  150. query.select(`.zp-swiper-${position}`).boundingClientRect(res => {
  151. this.$set(this.swiperContentStyle, position, res ? res.width + 'px' : '0px');
  152. }).exec();
  153. })
  154. }, delayTime)
  155. })
  156. }
  157. }
  158. }
  159. </script>
  160. <style scoped>
  161. .zp-swiper-container {
  162. /* #ifndef APP-NVUE */
  163. display: flex;
  164. /* #endif */
  165. flex-direction: column;
  166. flex: 1;
  167. }
  168. .zp-swiper-container-fixed {
  169. position: fixed;
  170. /* #ifndef APP-NVUE */
  171. height: auto;
  172. width: auto;
  173. /* #endif */
  174. top: 0;
  175. left: 0;
  176. bottom: 0;
  177. right: 0;
  178. }
  179. .zp-safe-area-inset-bottom {
  180. position: absolute;
  181. /* #ifndef APP-PLUS */
  182. height: env(safe-area-inset-bottom);
  183. /* #endif */
  184. }
  185. .zp-swiper-super {
  186. flex: 1;
  187. position: relative;
  188. /* #ifndef APP-NVUE */
  189. display: flex;
  190. /* #endif */
  191. flex-direction: row;
  192. }
  193. .zp-swiper-left,.zp-swiper-right{
  194. /* #ifndef APP-NVUE */
  195. height: 100%;
  196. /* #endif */
  197. }
  198. .zp-swiper {
  199. flex: 1;
  200. /* #ifndef APP-NVUE */
  201. height: 100%;
  202. width: 100%;
  203. /* #endif */
  204. }
  205. .zp-absoulte {
  206. /* #ifndef APP-NVUE */
  207. position: absolute;
  208. top: 0;
  209. width: auto;
  210. /* #endif */
  211. }
  212. .zp-right{
  213. right: 0;
  214. }
  215. .zp-swiper-item {
  216. height: 100%;
  217. }
  218. </style>