u-loadmore.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <view class="u-load-more-wrap" :style="{
  3. backgroundColor: bgColor,
  4. marginBottom: marginBottom + 'rpx',
  5. marginTop: marginTop + 'rpx',
  6. height: $u.addUnit(height)
  7. }">
  8. <u-line color="#d4d4d4" length="50" class="u-line"></u-line>
  9. <!-- 加载中和没有更多的状态才显示两边的横线 -->
  10. <view :class="status == 'loadmore' || status == 'nomore' ? 'u-more' : ''" class="u-load-more-inner">
  11. <view class="u-loadmore-icon-wrap">
  12. <u-loading class="u-loadmore-icon" :color="iconColor" :mode="iconType == 'circle' ? 'circle' : 'flower'" :show="status == 'loading' && icon"></u-loading>
  13. </view>
  14. <!-- 如果没有更多的状态下,显示内容为dot(粗点),加载特定样式 -->
  15. <view class="u-line-1" :style="[loadTextStyle]" :class="[(status == 'nomore' && isDot == true) ? 'u-dot-text' : 'u-more-text']" @tap="loadMore">
  16. {{ showText }}
  17. </view>
  18. </view>
  19. <u-line color="#d4d4d4" length="50" class="u-line"></u-line>
  20. </view>
  21. </template>
  22. <script>
  23. /**
  24. * loadmore 加载更多
  25. * @description 此组件一般用于标识页面底部加载数据时的状态。
  26. * @tutorial https://www.uviewui.com/components/loadMore.html
  27. * @property {String} status 组件状态(默认loadmore)
  28. * @property {String} bg-color 组件背景颜色,在页面是非白色时会用到(默认#ffffff)
  29. * @property {Boolean} icon 加载中时是否显示图标(默认true)
  30. * @property {String} icon-type 加载中时的图标类型(默认circle)
  31. * @property {String} icon-color icon-type为circle时有效,加载中的动画图标的颜色(默认#b7b7b7)
  32. * @property {Boolean} is-dot status为nomore时,内容显示为一个"●"(默认false)
  33. * @property {String} color 字体颜色(默认#606266)
  34. * @property {String Number} margin-top 到上一个相邻元素的距离
  35. * @property {String Number} margin-bottom 到下一个相邻元素的距离
  36. * @property {Object} load-text 自定义显示的文字,见上方说明示例
  37. * @event {Function} loadmore status为loadmore时,点击组件会发出此事件
  38. * @example <u-loadmore :status="status" icon-type="iconType" load-text="loadText" />
  39. */
  40. export default {
  41. name: "u-loadmore",
  42. emits: ["loadmore"],
  43. props: {
  44. // 组件背景色
  45. bgColor: {
  46. type: String,
  47. default: 'transparent'
  48. },
  49. // 是否显示加载中的图标
  50. icon: {
  51. type: Boolean,
  52. default: true
  53. },
  54. // 字体大小
  55. fontSize: {
  56. type: String,
  57. default: '28'
  58. },
  59. // 字体颜色
  60. color: {
  61. type: String,
  62. default: '#606266'
  63. },
  64. // 组件状态,loadmore-加载前的状态,loading-加载中的状态,nomore-没有更多的状态
  65. status: {
  66. type: String,
  67. default: 'loadmore'
  68. },
  69. // 加载中状态的图标,flower-花朵状图标,circle-圆圈状图标
  70. iconType: {
  71. type: String,
  72. default: 'circle'
  73. },
  74. // 显示的文字
  75. loadText: {
  76. type: Object,
  77. default () {
  78. return {
  79. loadmore: '加载更多',
  80. loading: '正在加载...',
  81. nomore: '没有更多了'
  82. }
  83. }
  84. },
  85. // 在“没有更多”状态下,是否显示粗点
  86. isDot: {
  87. type: Boolean,
  88. default: false
  89. },
  90. // 加载中显示圆圈动画时,动画的颜色
  91. iconColor: {
  92. type: String,
  93. default: '#b7b7b7'
  94. },
  95. // 上边距
  96. marginTop: {
  97. type: [String, Number],
  98. default: 0
  99. },
  100. // 下边距
  101. marginBottom: {
  102. type: [String, Number],
  103. default: 0
  104. },
  105. // 高度,单位rpx
  106. height: {
  107. type: [String, Number],
  108. default: 'auto'
  109. }
  110. },
  111. data() {
  112. return {
  113. // 粗点
  114. dotText: "●"
  115. }
  116. },
  117. computed: {
  118. // 加载的文字显示的样式
  119. loadTextStyle() {
  120. return {
  121. color: this.color,
  122. fontSize: this.fontSize + 'rpx',
  123. position: 'relative',
  124. zIndex: 1,
  125. backgroundColor: this.bgColor,
  126. // 如果是加载中状态,动画和文字需要距离近一点
  127. }
  128. },
  129. // 加载中圆圈动画的样式
  130. cricleStyle() {
  131. return {
  132. borderColor: `#e5e5e5 #e5e5e5 #e5e5e5 ${this.circleColor}`
  133. }
  134. },
  135. // 加载中花朵动画形式
  136. // 动画由base64图片生成,暂不支持修改
  137. flowerStyle() {
  138. return {
  139. }
  140. },
  141. // 显示的提示文字
  142. showText() {
  143. let text = '';
  144. if(this.status == 'loadmore') text = this.loadText.loadmore;
  145. else if(this.status == 'loading') text = this.loadText.loading;
  146. else if(this.status == 'nomore' && this.isDot) text = this.dotText;
  147. else text = this.loadText.nomore;
  148. return text;
  149. }
  150. },
  151. methods: {
  152. loadMore() {
  153. // 只有在“加载更多”的状态下才发送点击事件,内容不满一屏时无法触发底部上拉事件,所以需要点击来触发
  154. if(this.status == 'loadmore') this.$emit('loadmore');
  155. }
  156. }
  157. }
  158. </script>
  159. <style scoped lang="scss">
  160. @import "../../libs/css/style.components.scss";
  161. /* #ifdef MP */
  162. // 在mp.scss中,赋予了u-line为flex: 1,这里需要一个明确的长度,所以重置掉它
  163. // 在组件内部,把组件名(u-line)当做选择器,在微信开发工具会提示不合法,但不影响使用
  164. .u-line {
  165. flex: none;
  166. }
  167. /* #endif */
  168. .u-load-more-wrap {
  169. @include vue-flex;
  170. justify-content: center;
  171. align-items: center;
  172. }
  173. .u-load-more-inner {
  174. @include vue-flex;
  175. justify-content: center;
  176. align-items: center;
  177. padding: 0 12rpx;
  178. }
  179. .u-more {
  180. position: relative;
  181. @include vue-flex;
  182. justify-content: center;
  183. }
  184. .u-dot-text {
  185. font-size: 28rpx;
  186. }
  187. .u-loadmore-icon-wrap {
  188. margin-right: 8rpx;
  189. }
  190. .u-loadmore-icon {
  191. @include vue-flex;
  192. align-items: center;
  193. justify-content: center;
  194. }
  195. </style>