z-paging-swiper.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. let 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){
  98. return 0;
  99. }
  100. let safeAreaBottom = 0;
  101. // #ifdef APP-PLUS
  102. safeAreaBottom = this.systemInfo.safeAreaInsets.bottom || 0;
  103. // #endif
  104. // #ifndef APP-PLUS
  105. safeAreaBottom = this.cssSafeAreaInsetBottom === -1 ? 0 : this.cssSafeAreaInsetBottom;
  106. // #endif
  107. return safeAreaBottom;
  108. },
  109. isOldWebView() {
  110. // #ifndef APP-NVUE
  111. try {
  112. const systemInfos = uni.getSystemInfoSync().system.split(' ');
  113. const deviceType = systemInfos[0];
  114. const version = parseInt(systemInfos[1].slice(0,1));
  115. if ((deviceType === 'iOS' && version <= 10) || (deviceType === 'Android' && version <= 6)) {
  116. return true;
  117. }
  118. } catch(e){
  119. return false;
  120. }
  121. // #endif
  122. return false;
  123. }
  124. },
  125. methods: {
  126. //通过获取css设置的底部安全区域占位view高度设置bottom距离
  127. _getCssSafeAreaInsetBottom(){
  128. const query = uni.createSelectorQuery().in(this);
  129. query.select('.zp-safe-area-inset-bottom').boundingClientRect(res => {
  130. if (res) {
  131. this.cssSafeAreaInsetBottom = res.height;
  132. }
  133. }).exec();
  134. },
  135. //获取slot="left"和slot="right"宽度并且更新布局
  136. _updateLeftAndRightWidth(){
  137. if (!this.isOldWebView) return;
  138. this.$nextTick(() => {
  139. let delayTime = 0;
  140. // #ifdef MP-BAIDU
  141. delayTime = 10;
  142. // #endif
  143. setTimeout(() => {
  144. const query = uni.createSelectorQuery().in(this);
  145. query.select('.zp-swiper-left').boundingClientRect(res => {
  146. this.$set(this.swiperContentStyle,'left',res ? res[0].width + 'px' : '0px');
  147. }).exec();
  148. query.select('.zp-swiper-right').boundingClientRect(res => {
  149. this.$set(this.swiperContentStyle,'right',res ? res[0].width + 'px' : '0px');
  150. }).exec();
  151. }, delayTime)
  152. })
  153. }
  154. }
  155. }
  156. </script>
  157. <style scoped>
  158. .zp-swiper-container {
  159. /* #ifndef APP-NVUE */
  160. display: flex;
  161. /* #endif */
  162. flex-direction: column;
  163. flex: 1;
  164. }
  165. .zp-swiper-container-fixed {
  166. position: fixed;
  167. /* #ifndef APP-NVUE */
  168. height: auto;
  169. width: auto;
  170. /* #endif */
  171. top: 0;
  172. left: 0;
  173. bottom: 0;
  174. right: 0;
  175. }
  176. .zp-safe-area-inset-bottom {
  177. position: absolute;
  178. /* #ifndef APP-PLUS */
  179. height: env(safe-area-inset-bottom);
  180. /* #endif */
  181. }
  182. .zp-swiper-super {
  183. flex: 1;
  184. position: relative;
  185. /* #ifndef APP-NVUE */
  186. display: flex;
  187. /* #endif */
  188. flex-direction: row;
  189. }
  190. .zp-swiper-left,.zp-swiper-right{
  191. /* #ifndef APP-NVUE */
  192. height: 100%;
  193. /* #endif */
  194. }
  195. .zp-swiper {
  196. flex: 1;
  197. /* #ifndef APP-NVUE */
  198. height: 100%;
  199. width: 100%;
  200. /* #endif */
  201. }
  202. .zp-absoulte {
  203. /* #ifndef APP-NVUE */
  204. position: absolute;
  205. top: 0;
  206. width: auto;
  207. /* #endif */
  208. }
  209. .zp-right{
  210. right: 0;
  211. }
  212. .zp-swiper-item {
  213. height: 100%;
  214. }
  215. </style>