u-switch.vue 6.9 KB

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