u-loadmore.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <view :style="{
  3. backgroundColor: bgColor,
  4. marginBottom: marginBottom + 'rpx',
  5. marginTop: marginTop + 'rpx',
  6. height: $u.addUnit(height)
  7. }" class="u-load-more-wrap">
  8. <u-line color="#d4d4d4" length="50"></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 :color="iconColor" :mode="iconType == 'circle' ? 'circle' : 'flower'" :show="status == 'loading' && icon"
  13. class="u-loadmore-icon"></u-loading>
  14. </view>
  15. <!-- 如果没有更多的状态下,显示内容为dot(粗点),加载特定样式 -->
  16. <view :class="[(status == 'nomore' && isDot == true) ? 'u-dot-text' : 'u-more-text']" :style="[loadTextStyle]"
  17. class="u-line-1" @tap="loadMore">
  18. {{ showText }}
  19. </view>
  20. </view>
  21. <u-line color="#d4d4d4" length="50"></u-line>
  22. </view>
  23. </template>
  24. <script>
  25. /**
  26. * loadmore 加载更多
  27. * @description 此组件一般用于标识页面底部加载数据时的状态。
  28. * @tutorial https://www.uviewui.com/components/loadMore.html
  29. * @property {String} status 组件状态(默认loadmore)
  30. * @property {String} bg-color 组件背景颜色,在页面是非白色时会用到(默认#ffffff)
  31. * @property {Boolean} icon 加载中时是否显示图标(默认true)
  32. * @property {String} icon-type 加载中时的图标类型(默认circle)
  33. * @property {String} icon-color icon-type为circle时有效,加载中的动画图标的颜色(默认#b7b7b7)
  34. * @property {Boolean} is-dot status为nomore时,内容显示为一个"●"(默认false)
  35. * @property {String} color 字体颜色(默认#606266)
  36. * @property {String Number} margin-top 到上一个相邻元素的距离
  37. * @property {String Number} margin-bottom 到下一个相邻元素的距离
  38. * @property {Object} load-text 自定义显示的文字,见上方说明示例
  39. * @event {Function} loadmore status为loadmore时,点击组件会发出此事件
  40. * @example <u-loadmore :status="status" icon-type="iconType" load-text="loadText" />
  41. */
  42. export default {
  43. name: "u-loadmore",
  44. props: {
  45. // 组件背景色
  46. bgColor: {
  47. type: String,
  48. default: 'transparent'
  49. },
  50. // 是否显示加载中的图标
  51. icon: {
  52. type: Boolean,
  53. default: true
  54. },
  55. // 字体大小
  56. fontSize: {
  57. type: String,
  58. default: '28'
  59. },
  60. // 字体颜色
  61. color: {
  62. type: String,
  63. default: '#606266'
  64. },
  65. // 组件状态,loadmore-加载前的状态,loading-加载中的状态,nomore-没有更多的状态
  66. status: {
  67. type: String,
  68. default: 'loadmore'
  69. },
  70. // 加载中状态的图标,flower-花朵状图标,circle-圆圈状图标
  71. iconType: {
  72. type: String,
  73. default: 'circle'
  74. },
  75. // 显示的文字
  76. loadText: {
  77. type: Object,
  78. default() {
  79. return {
  80. loadmore: '加载更多',
  81. loading: '正在加载...',
  82. nomore: '没有更多了'
  83. }
  84. }
  85. },
  86. // 在“没有更多”状态下,是否显示粗点
  87. isDot: {
  88. type: Boolean,
  89. default: false
  90. },
  91. // 加载中显示圆圈动画时,动画的颜色
  92. iconColor: {
  93. type: String,
  94. default: '#b7b7b7'
  95. },
  96. // 上边距
  97. marginTop: {
  98. type: [String, Number],
  99. default: 0
  100. },
  101. // 下边距
  102. marginBottom: {
  103. type: [String, Number],
  104. default: 0
  105. },
  106. // 高度,单位rpx
  107. height: {
  108. type: [String, Number],
  109. default: 'auto'
  110. }
  111. },
  112. data() {
  113. return {
  114. // 粗点
  115. dotText: "●"
  116. }
  117. },
  118. computed: {
  119. // 加载的文字显示的样式
  120. loadTextStyle() {
  121. return {
  122. color: this.color,
  123. fontSize: this.fontSize + 'rpx',
  124. position: 'relative',
  125. zIndex: 1,
  126. backgroundColor: this.bgColor,
  127. // 如果是加载中状态,动画和文字需要距离近一点
  128. }
  129. },
  130. // 加载中圆圈动画的样式
  131. cricleStyle() {
  132. return {
  133. borderColor: `#e5e5e5 #e5e5e5 #e5e5e5 ${this.circleColor}`
  134. }
  135. },
  136. // 加载中花朵动画形式
  137. // 动画由base64图片生成,暂不支持修改
  138. flowerStyle() {
  139. return {}
  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 lang="scss" scoped>
  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 12 rpx;
  178. }
  179. .u-more {
  180. position: relative;
  181. @include vue-flex;
  182. justify-content: center;
  183. }
  184. .u-dot-text {
  185. font-size: 28 rpx;
  186. }
  187. .u-loadmore-icon-wrap {
  188. margin-right: 8 rpx;
  189. }
  190. .u-loadmore-icon {
  191. @include vue-flex;
  192. align-items: center;
  193. justify-content: center;
  194. }
  195. </style>