u-loadmore.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view
  3. class="u-loadmore"
  4. :style="[
  5. $u.addStyle(customStyle),
  6. {
  7. backgroundColor: bgColor,
  8. marginBottom: $u.addUnit(marginBottom),
  9. marginTop: $u.addUnit(marginTop),
  10. height: $u.addUnit(height)
  11. }
  12. ]"
  13. >
  14. <u-line
  15. length="140rpx"
  16. :color="lineColor"
  17. :hairline="false"
  18. :dashed="dashed"
  19. v-if="line"
  20. ></u-line>
  21. <!-- 加载中和没有更多的状态才显示两边的横线 -->
  22. <view
  23. :class="status == 'loadmore' || status == 'nomore' ? 'u-more' : ''"
  24. class="u-loadmore__content"
  25. >
  26. <view
  27. class="u-loadmore__content__icon-wrap"
  28. v-if="status === 'loading' && icon"
  29. >
  30. <u-loading-icon
  31. :color="iconColor"
  32. :size="iconSize"
  33. :mode="loadingIcon"
  34. ></u-loading-icon>
  35. </view>
  36. <!-- 如果没有更多的状态下,显示内容为dot(粗点),加载特定样式 -->
  37. <text
  38. class="u-line-1"
  39. :style="[loadTextStyle]"
  40. :class="[
  41. status == 'nomore' && isDot == true
  42. ? 'u-loadmore__content__dot-text'
  43. : 'u-loadmore__content__text'
  44. ]"
  45. @tap="loadMore"
  46. >{{ showText }}</text
  47. >
  48. </view>
  49. <u-line
  50. length="140rpx"
  51. :color="lineColor"
  52. :hairline="false"
  53. :dashed="dashed"
  54. v-if="line"
  55. ></u-line>
  56. </view>
  57. </template>
  58. <script>
  59. import props from './props.js'
  60. /**
  61. * loadmore 加载更多
  62. * @description 此组件一般用于标识页面底部加载数据时的状态。
  63. * @tutorial https://www.uviewui.com/components/loadMore.html
  64. * @property {String} status 组件状态(默认 'loadmore' )
  65. * @property {String} bgColor 组件背景颜色,在页面是非白色时会用到(默认 'transparent' )
  66. * @property {Boolean} icon 加载中时是否显示图标(默认 true )
  67. * @property {String | Number} fontSize 字体大小(默认 14 )
  68. * @property {String | Number} iconSize 图标大小(默认 17 )
  69. * @property {String} color 字体颜色(默认 '#606266' )
  70. * @property {String} loadingIcon 加载图标(默认 'circle' )
  71. * @property {String} loadmoreText 加载前的提示语(默认 '加载更多' )
  72. * @property {String} loadingText 加载中提示语(默认 '正在加载...' )
  73. * @property {String} nomoreText 没有更多的提示语(默认 '没有更多了' )
  74. * @property {Boolean} isDot 到上一个相邻元素的距离 (默认 false )
  75. * @property {String} iconColor 加载中图标的颜色 (默认 '#b7b7b7' )
  76. * @property {String} lineColor 线条颜色(默认 #E6E8EB )
  77. * @property {String | Number} marginTop 上边距 (默认 10 )
  78. * @property {String | Number} marginBottom 下边距 (默认 10 )
  79. * @property {String | Number} height 高度,单位px (默认 'auto' )
  80. * @property {Boolean} line 是否显示左边分割线 (默认 false )
  81. * @property {Boolean} dashed // 是否虚线,true-虚线,false-实线 (默认 false )
  82. * @event {Function} loadmore status为loadmore时,点击组件会发出此事件
  83. * @example <u-loadmore :status="status" icon-type="iconType" load-text="loadText" />
  84. */
  85. export default {
  86. name: 'u-loadmore',
  87. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  88. data () {
  89. return {
  90. // 粗点
  91. dotText: '●'
  92. }
  93. },
  94. computed: {
  95. // 加载的文字显示的样式
  96. loadTextStyle () {
  97. return {
  98. color: this.color,
  99. fontSize: uni.$u.addUnit(this.fontSize),
  100. lineHeight: uni.$u.addUnit(this.fontSize),
  101. backgroundColor: this.bgColor
  102. }
  103. },
  104. // 显示的提示文字
  105. showText () {
  106. let text = ''
  107. if (this.status == 'loadmore') text = this.loadmoreText
  108. else if (this.status == 'loading') text = this.loadingText
  109. else if (this.status == 'nomore' && this.isDot) text = this.dotText
  110. else text = this.nomoreText
  111. return text
  112. }
  113. },
  114. methods: {
  115. loadMore () {
  116. // 只有在“加载更多”的状态下才发送点击事件,内容不满一屏时无法触发底部上拉事件,所以需要点击来触发
  117. if (this.status == 'loadmore') this.$emit('loadmore')
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss" scoped>
  123. @import '../../libs/css/components.scss';
  124. .u-loadmore {
  125. @include flex(row);
  126. align-items: center;
  127. justify-content: center;
  128. flex: 1;
  129. &__content {
  130. margin: 0 15px;
  131. @include flex(row);
  132. align-items: center;
  133. justify-content: center;
  134. &__icon-wrap {
  135. margin-right: 8px;
  136. }
  137. &__text {
  138. font-size: 32rpx;
  139. color: $u-content-color;
  140. }
  141. &__dot-text {
  142. font-size: 15px;
  143. color: $u-tips-color;
  144. }
  145. }
  146. }
  147. </style>