u-switch.vue 6.7 KB

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