u-loadmore.vue 6.5 KB

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