u-switch.vue 6.1 KB

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