3
0

u-loading-icon.vue 12 KB

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