u-loading-icon.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <template>
  2. <view
  3. class="u-loading-icon"
  4. :style="[$u.addStyle(customStyle)]"
  5. :class="[vertical && 'u-loading-icon--vertical']"
  6. v-if="show"
  7. >
  8. <view
  9. v-if="!webviewHide"
  10. class="u-loading-icon__spinner"
  11. :class="[`u-loading-icon__spinner--${mode}`]"
  12. ref="ani"
  13. :style="{
  14. color: color,
  15. width: $u.addUnit(size),
  16. height: $u.addUnit(size),
  17. borderTopColor: color,
  18. borderBottomColor: otherBorderColor,
  19. borderLeftColor: otherBorderColor,
  20. borderRightColor: otherBorderColor,
  21. 'animation-duration': `${duration}ms`,
  22. 'animation-timing-function': mode === 'semicircle' || mode === 'circle' ? timingFunction : ''
  23. }"
  24. >
  25. <block v-if="mode === 'spinner'">
  26. <!-- #ifndef APP-NVUE -->
  27. <view
  28. v-for="(item, index) in array12"
  29. :key="index"
  30. class="u-loading-icon__dot"
  31. >
  32. </view>
  33. <!-- #endif -->
  34. <!-- #ifdef APP-NVUE -->
  35. <!-- 此组件内部图标部分无法设置宽高,即使通过width和height配置了也无效 -->
  36. <loading-indicator
  37. v-if="!webviewHide"
  38. class="u-loading-indicator"
  39. :animating="true"
  40. :style="{
  41. color: color,
  42. width: $u.addUnit(size),
  43. height: $u.addUnit(size)
  44. }"
  45. />
  46. <!-- #endif -->
  47. </block>
  48. </view>
  49. <text
  50. v-if="text"
  51. class="u-loading-icon__text"
  52. :style="{
  53. fontSize: $u.addUnit(textSize),
  54. color: textColor,
  55. }"
  56. >{{ text }}
  57. </text>
  58. </view>
  59. </template>
  60. <script>
  61. import props from './props.js';
  62. // #ifdef APP-NVUE
  63. const animation = weex.requireModule('animation');
  64. // #endif
  65. /**
  66. * loading 加载动画
  67. * @description 警此组件为一个小动画,目前用在uView的loadmore加载更多和switch开关等组件的正在加载状态场景。
  68. * @tutorial https://www.uviewui.com/components/loading.html
  69. * @property {Boolean} show 是否显示组件 (默认 true)
  70. * @property {String} color 动画活动区域的颜色,只对 mode = flower 模式有效(默认color['u-tips-color'])
  71. * @property {String} textColor 提示文本的颜色(默认color['u-tips-color'])
  72. * @property {Boolean} vertical 文字和图标是否垂直排列 (默认 false )
  73. * @property {String} mode 模式选择,见官网说明(默认 'circle' )
  74. * @property {String | Number} size 加载图标的大小,单位px (默认 24 )
  75. * @property {String | Number} textSize 文字大小(默认 15 )
  76. * @property {String | Number} text 文字内容
  77. * @property {String} timingFunction 动画模式 (默认 'ease-in-out' )
  78. * @property {String | Number} duration 动画执行周期时间(默认 1200)
  79. * @property {String} inactiveColor mode=circle时的暗边颜色
  80. * @property {Object} customStyle 定义需要用到的外部样式
  81. * @example <u-loading mode="circle"></u-loading>
  82. */
  83. export default {
  84. name: 'u-loading-icon',
  85. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  86. data() {
  87. return {
  88. // Array.form可以通过一个伪数组对象创建指定长度的数组
  89. // https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/from
  90. array12: Array.from({
  91. length: 12
  92. }),
  93. // 这里需要设置默认值为360,否则在安卓nvue上,会延迟一个duration周期后才执行
  94. // 在iOS nvue上,则会一开始默认执行两个周期的动画
  95. aniAngel: 360, // 动画旋转角度
  96. webviewHide: false, // 监听webview的状态,如果隐藏了页面,则停止动画,以免性能消耗
  97. loading: false, // 是否运行中,针对nvue使用
  98. }
  99. },
  100. computed: {
  101. // 当为circle类型时,给其另外三边设置一个更轻一些的颜色
  102. // 之所以需要这么做的原因是,比如父组件传了color为红色,那么需要另外的三个边为浅红色
  103. // 而不能是固定的某一个其他颜色(因为这个固定的颜色可能浅蓝,导致效果没有那么细腻良好)
  104. otherBorderColor() {
  105. const lightColor = uni.$u.colorGradient(this.color, '#ffffff', 100)[80]
  106. if (this.mode === 'circle') {
  107. return this.inactiveColor ? this.inactiveColor : lightColor
  108. } else {
  109. return 'transparent'
  110. }
  111. // return this.mode === 'circle' ? this.inactiveColor ? this.inactiveColor : lightColor : 'transparent'
  112. }
  113. },
  114. watch: {
  115. show(n) {
  116. // nvue中,show为true,且为非loading状态,就重新执行动画模块
  117. // #ifdef APP-NVUE
  118. if (n && !this.loading) {
  119. setTimeout(() => {
  120. this.startAnimate()
  121. }, 30)
  122. }
  123. // #endif
  124. }
  125. },
  126. mounted() {
  127. this.init()
  128. },
  129. methods: {
  130. init() {
  131. setTimeout(() => {
  132. // #ifdef APP-NVUE
  133. this.show && this.nvueAnimate()
  134. // #endif
  135. // #ifdef APP-PLUS
  136. this.show && this.addEventListenerToWebview()
  137. // #endif
  138. }, 20)
  139. },
  140. // 监听webview的显示与隐藏
  141. addEventListenerToWebview() {
  142. // webview的堆栈
  143. const pages = getCurrentPages()
  144. // 当前页面
  145. const page = pages[pages.length - 1]
  146. // 当前页面的webview实例
  147. const currentWebview = page.$getAppWebview()
  148. // 监听webview的显示与隐藏,从而停止或者开始动画(为了性能)
  149. currentWebview.addEventListener('hide', () => {
  150. this.webviewHide = true
  151. })
  152. currentWebview.addEventListener('show', () => {
  153. this.webviewHide = false
  154. })
  155. },
  156. // #ifdef APP-NVUE
  157. nvueAnimate() {
  158. // nvue下,非spinner类型时才需要旋转,因为nvue的spinner类型,使用了weex的
  159. // loading-indicator组件,自带旋转功能
  160. this.mode !== 'spinner' && this.startAnimate()
  161. },
  162. // 执行nvue的animate模块动画
  163. startAnimate() {
  164. this.loading = true
  165. const ani = this.$refs.ani
  166. if (!ani) return
  167. animation.transition(ani, {
  168. // 进行角度旋转
  169. styles: {
  170. transform: `rotate(${this.aniAngel}deg)`,
  171. transformOrigin: 'center center'
  172. },
  173. duration: this.duration,
  174. timingFunction: this.timingFunction,
  175. // delay: 10
  176. }, () => {
  177. // 每次增加360deg,为了让其重新旋转一周
  178. this.aniAngel += 360
  179. // 动画结束后,继续循环执行动画,需要同时判断webviewHide变量
  180. // nvue安卓,页面隐藏后依然会继续执行startAnimate方法
  181. this.show && !this.webviewHide ? this.startAnimate() : this.loading = false
  182. })
  183. }
  184. // #endif
  185. }
  186. }
  187. </script>
  188. <style lang="scss" scoped>
  189. @import "../../libs/css/components.scss";
  190. $u-loading-icon-color: #c8c9cc !default;
  191. $u-loading-icon-text-margin-left: 4px !default;
  192. $u-loading-icon-text-color: $u-content-color !default;
  193. $u-loading-icon-text-font-size: 14px !default;
  194. $u-loading-icon-text-line-height: 20px !default;
  195. $u-loading-width: 30px !default;
  196. $u-loading-height: 30px !default;
  197. $u-loading-max-width: 100% !default;
  198. $u-loading-max-height: 100% !default;
  199. $u-loading-semicircle-border-width: 2px !default;
  200. $u-loading-semicircle-border-color: transparent !default;
  201. $u-loading-semicircle-border-top-right-radius: 100px !default;
  202. $u-loading-semicircle-border-top-left-radius: 100px !default;
  203. $u-loading-semicircle-border-bottom-left-radius: 100px !default;
  204. $u-loading-semicircle-border-bottom-right-radiu: 100px !default;
  205. $u-loading-semicircle-border-style: solid !default;
  206. $u-loading-circle-border-top-right-radius: 100px !default;
  207. $u-loading-circle-border-top-left-radius: 100px !default;
  208. $u-loading-circle-border-bottom-left-radius: 100px !default;
  209. $u-loading-circle-border-bottom-right-radiu: 100px !default;
  210. $u-loading-circle-border-width: 2px !default;
  211. $u-loading-circle-border-top-color: #e5e5e5 !default;
  212. $u-loading-circle-border-right-color: $u-loading-circle-border-top-color !default;
  213. $u-loading-circle-border-bottom-color: $u-loading-circle-border-top-color !default;
  214. $u-loading-circle-border-left-color: $u-loading-circle-border-top-color !default;
  215. $u-loading-circle-border-style: solid !default;
  216. $u-loading-icon-host-font-size: 0px !default;
  217. $u-loading-icon-host-line-height: 1 !default;
  218. $u-loading-icon-vertical-margin: 6px 0 0 !default;
  219. $u-loading-icon-dot-top: 0 !default;
  220. $u-loading-icon-dot-left: 0 !default;
  221. $u-loading-icon-dot-width: 100% !default;
  222. $u-loading-icon-dot-height: 100% !default;
  223. $u-loading-icon-dot-before-width: 2px !default;
  224. $u-loading-icon-dot-before-height: 25% !default;
  225. $u-loading-icon-dot-before-margin: 0 auto !default;
  226. $u-loading-icon-dot-before-background-color: currentColor !default;
  227. $u-loading-icon-dot-before-border-radius: 40% !default;
  228. .u-loading-icon {
  229. /* #ifndef APP-NVUE */
  230. // display: inline-flex;
  231. /* #endif */
  232. flex-direction: row;
  233. align-items: center;
  234. justify-content: center;
  235. color: $u-loading-icon-color;
  236. &__text {
  237. margin-left: $u-loading-icon-text-margin-left;
  238. color: $u-loading-icon-text-color;
  239. font-size: $u-loading-icon-text-font-size;
  240. line-height: $u-loading-icon-text-line-height;
  241. }
  242. &__spinner {
  243. width: $u-loading-width;
  244. height: $u-loading-height;
  245. position: relative;
  246. /* #ifndef APP-NVUE */
  247. box-sizing: border-box;
  248. max-width: $u-loading-max-width;
  249. max-height: $u-loading-max-height;
  250. animation: u-rotate 1s linear infinite;
  251. /* #endif */
  252. }
  253. &__spinner--semicircle {
  254. border-width: $u-loading-semicircle-border-width;
  255. border-color: $u-loading-semicircle-border-color;
  256. border-top-right-radius: $u-loading-semicircle-border-top-right-radius;
  257. border-top-left-radius: $u-loading-semicircle-border-top-left-radius;
  258. border-bottom-left-radius: $u-loading-semicircle-border-bottom-left-radius;
  259. border-bottom-right-radius: $u-loading-semicircle-border-bottom-right-radiu;
  260. border-style: $u-loading-semicircle-border-style;
  261. }
  262. &__spinner--circle {
  263. border-top-right-radius: $u-loading-circle-border-top-right-radius;
  264. border-top-left-radius: $u-loading-circle-border-top-left-radius;
  265. border-bottom-left-radius: $u-loading-circle-border-bottom-left-radius;
  266. border-bottom-right-radius: $u-loading-circle-border-bottom-right-radiu;
  267. border-width: $u-loading-circle-border-width;
  268. border-top-color: $u-loading-circle-border-top-color;
  269. border-right-color: $u-loading-circle-border-right-color;
  270. border-bottom-color: $u-loading-circle-border-bottom-color;
  271. border-left-color: $u-loading-circle-border-left-color;
  272. border-style: $u-loading-circle-border-style;
  273. }
  274. &--vertical {
  275. flex-direction: column
  276. }
  277. }
  278. /* #ifndef APP-NVUE */
  279. :host {
  280. font-size: $u-loading-icon-host-font-size;
  281. line-height: $u-loading-icon-host-line-height;
  282. }
  283. .u-loading-icon {
  284. &__spinner--spinner {
  285. animation-timing-function: steps(12)
  286. }
  287. &__text:empty {
  288. display: none
  289. }
  290. &--vertical &__text {
  291. margin: $u-loading-icon-vertical-margin;
  292. color: $u-content-color;
  293. }
  294. &__dot {
  295. position: absolute;
  296. top: $u-loading-icon-dot-top;
  297. left: $u-loading-icon-dot-left;
  298. width: $u-loading-icon-dot-width;
  299. height: $u-loading-icon-dot-height;
  300. &:before {
  301. display: block;
  302. width: $u-loading-icon-dot-before-width;
  303. height: $u-loading-icon-dot-before-height;
  304. margin: $u-loading-icon-dot-before-margin;
  305. background-color: $u-loading-icon-dot-before-background-color;
  306. border-radius: $u-loading-icon-dot-before-border-radius;
  307. content: " "
  308. }
  309. }
  310. }
  311. @for $i from 1 through 12 {
  312. .u-loading-icon__dot:nth-of-type(#{$i}) {
  313. transform: rotate($i * 30deg);
  314. opacity: 1 - 0.0625 * ($i - 1);
  315. }
  316. }
  317. @keyframes u-rotate {
  318. 0% {
  319. transform: rotate(0deg)
  320. }
  321. to {
  322. transform: rotate(1turn)
  323. }
  324. }
  325. /* #endif */
  326. </style>