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