u-switch.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <view
  3. class="u-switch"
  4. :class="[disabled && 'u-switch--disabled']"
  5. :style="[switchStyle, $u.addStyle(customStyle)]"
  6. @tap="clickHandler"
  7. >
  8. <view
  9. class="u-switch__bg"
  10. :style="[bgStyle]"
  11. >
  12. </view>
  13. <view
  14. class="u-switch__node"
  15. :class="[value && 'u-switch__node--on']"
  16. :style="[nodeStyle]"
  17. ref="u-switch__node"
  18. >
  19. <u-loading-icon
  20. :show="loading"
  21. mode="circle"
  22. timingFunction='linear'
  23. :color="value ? activeColor : '#AAABAD'"
  24. :size="size * 0.6"
  25. />
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import props from './props.js';
  31. /**
  32. * switch 开关选择器
  33. * @description 选择开关一般用于只有两个选择,且只能选其一的场景。
  34. * @tutorial https://www.uviewui.com/components/switch.html
  35. * @property {Boolean} loading 是否处于加载中(默认 false )
  36. * @property {Boolean} disabled 是否禁用(默认 false )
  37. * @property {String | Number} size 开关尺寸,单位px (默认 25 )
  38. * @property {String} activeColor 打开时的背景色 (默认 '#2979ff' )
  39. * @property {String} inactiveColor 关闭时的背景色 (默认 '#ffffff' )
  40. * @property {Boolean | String | Number} value 通过v-model双向绑定的值 (默认 false )
  41. * @property {Boolean | String | Number} activeValue 打开选择器时通过change事件发出的值 (默认 true )
  42. * @property {Boolean | String | Number} inactiveValue 关闭选择器时通过change事件发出的值 (默认 false )
  43. * @property {Boolean} asyncChange 是否开启异步变更,开启后需要手动控制输入值 (默认 false )
  44. * @property {String | Number} space 圆点与外边框的距离 (默认 0 )
  45. * @property {Object} customStyle 定义需要用到的外部样式
  46. *
  47. * @event {Function} change 在switch打开或关闭时触发
  48. * @example <u-switch v-model="checked" active-color="red" inactive-color="#eee"></u-switch>
  49. */
  50. export default {
  51. name: "u-switch",
  52. mixins: [uni.$u.mpMixin, uni.$u.mixin,props],
  53. watch: {
  54. value: {
  55. immediate: true,
  56. handler(n) {
  57. if(n !== this.inactiveValue && n !== this.activeValue) {
  58. uni.$u.error('v-mode绑定的值必须为inactiveValue、activeValue二者之一')
  59. }
  60. }
  61. }
  62. },
  63. data() {
  64. return {
  65. bgColor: '#ffffff'
  66. }
  67. },
  68. computed: {
  69. switchStyle() {
  70. let style = {}
  71. // 这里需要加2,是为了腾出边框的距离,否则圆点node会和外边框紧贴在一起
  72. style.width = uni.$u.addUnit(this.size * 2 + 2)
  73. style.height = uni.$u.addUnit(Number(this.size) + 2)
  74. // style.borderColor = this.value ? 'rgba(0, 0, 0, 0)' : 'rgba(0, 0, 0, 0.12)'
  75. // 如果自定义了“非激活”演示,name边框颜色设置为透明(跟非激活颜色一致)
  76. // 这里不能简单的设置为非激活的颜色,否则打开状态时,会有边框,所以需要透明
  77. if(this.customInactiveColor) {
  78. style.borderColor = 'rgba(0, 0, 0, 0)'
  79. }
  80. style.backgroundColor = this.value === this.activeValue ? this.activeColor : this.inactiveColor
  81. return style;
  82. },
  83. nodeStyle() {
  84. let style = {}
  85. // 如果自定义非激活颜色,将node圆点的尺寸减少两个像素,让其与外边框距离更大一点
  86. style.width = uni.$u.addUnit(this.size - this.space)
  87. style.height = uni.$u.addUnit(this.size - this.space)
  88. style.transform = `translateX(${this.value === this.activeValue ? -this.space : -this.size}px)`
  89. return style
  90. },
  91. bgStyle() {
  92. let style = {}
  93. // 这里配置一个多余的元素在HTML中,是为了让switch切换时,有更良好的背景色扩充体验(见实际效果)
  94. style.width = uni.$u.addUnit(Number(this.size) * 2 - this.size / 2)
  95. style.height = uni.$u.addUnit(this.size)
  96. style.backgroundColor = this.inactiveColor
  97. // 打开时,让此元素收缩,否则反之
  98. style.transform = `scale(${this.value === this.activeValue ? 0 : 1})`
  99. return style
  100. },
  101. customInactiveColor() {
  102. // 之所以需要判断是否自定义了“非激活”颜色,是为了让node圆点离外边框更宽一点的距离
  103. return this.inactiveColor !== '#fff' && this.inactiveColor !== '#ffffff'
  104. }
  105. },
  106. methods: {
  107. clickHandler() {
  108. if (!this.disabled && !this.loading) {
  109. const oldValue = this.value === this.activeValue ? this.inactiveValue : this.activeValue
  110. if(!this.asyncChange) {
  111. this.$emit('input', oldValue)
  112. }
  113. // 放到下一个生命周期,因为双向绑定的value修改父组件状态需要时间,且是异步的
  114. this.$nextTick(() => {
  115. this.$emit('change', oldValue)
  116. })
  117. }
  118. }
  119. }
  120. };
  121. </script>
  122. <style lang="scss" scoped>
  123. @import "../../libs/css/components.scss";
  124. .u-switch {
  125. @include flex(row);
  126. /* #ifndef APP-NVUE */
  127. box-sizing: border-box;
  128. /* #endif */
  129. position: relative;
  130. background-color: #fff;
  131. border-width: 1px;
  132. border-radius: 100px;
  133. transition: background-color 0.4s;
  134. border-color: rgba(0, 0, 0, 0.12);
  135. border-style: solid;
  136. justify-content: flex-end;
  137. align-items: center;
  138. // 由于weex为阿里逗着玩的KPI项目,导致bug奇多,这必须要写这一行,
  139. // 否则在iOS上,点击页面任意地方,都会触发switch的点击事件
  140. overflow: hidden;
  141. &__node {
  142. @include flex(row);
  143. align-items: center;
  144. justify-content: center;
  145. border-radius: 100px;
  146. background-color: #fff;
  147. border-radius: 100px;
  148. box-shadow: 1px 1px 1px 0 rgba(0, 0, 0, 0.25);
  149. transition-property: transform;
  150. transition-duration: 0.4s;
  151. transition-timing-function: cubic-bezier(0.3, 1.05, 0.4, 1.05);
  152. }
  153. &__bg {
  154. position: absolute;
  155. border-radius: 100px;
  156. background-color: #FFFFFF;
  157. transition-property: transform;
  158. transition-duration: 0.4s;
  159. border-top-left-radius: 0;
  160. border-bottom-left-radius: 0;
  161. transition-timing-function: ease;
  162. }
  163. &--disabled {
  164. opacity: 0.6;
  165. }
  166. }
  167. </style>