u-switch.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <view class="u-switch" :class="[disabled && 'u-switch--disabled']" :style="[switchStyle, $u.addStyle(customStyle)]"
  3. @tap="clickHandler">
  4. <view class="u-switch__bg" :style="[bgStyle]">
  5. </view>
  6. <view class="u-switch__node" :class="[value && 'u-switch__node--on']" :style="[nodeStyle]" ref="u-switch__node">
  7. <u-loading-icon :show="loading" mode="circle" timingFunction='linear'
  8. :color="value ? activeColor : '#AAABAD'" :size="size * 0.6" />
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. import props from './props.js';
  14. /**
  15. * switch 开关选择器
  16. * @description 选择开关一般用于只有两个选择,且只能选其一的场景。
  17. * @tutorial https://www.uviewui.com/components/switch.html
  18. * @property {Boolean} loading 是否处于加载中(默认 false )
  19. * @property {Boolean} disabled 是否禁用(默认 false )
  20. * @property {String | Number} size 开关尺寸,单位px (默认 25 )
  21. * @property {String} activeColor 打开时的背景色 (默认 '#2979ff' )
  22. * @property {String} inactiveColor 关闭时的背景色 (默认 '#ffffff' )
  23. * @property {Boolean | String | Number} value 通过v-model双向绑定的值 (默认 false )
  24. * @property {Boolean | String | Number} activeValue 打开选择器时通过change事件发出的值 (默认 true )
  25. * @property {Boolean | String | Number} inactiveValue 关闭选择器时通过change事件发出的值 (默认 false )
  26. * @property {Boolean} asyncChange 是否开启异步变更,开启后需要手动控制输入值 (默认 false )
  27. * @property {String | Number} space 圆点与外边框的距离 (默认 0 )
  28. * @property {Object} customStyle 定义需要用到的外部样式
  29. *
  30. * @event {Function} change 在switch打开或关闭时触发
  31. * @example <u-switch v-model="checked" active-color="red" inactive-color="#eee"></u-switch>
  32. */
  33. export default {
  34. name: "u-switch",
  35. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  36. watch: {
  37. value: {
  38. immediate: true,
  39. handler(n) {
  40. if (n !== this.inactiveValue && n !== this.activeValue) {
  41. uni.$u.error('v-model绑定的值必须为inactiveValue、activeValue二者之一')
  42. }
  43. }
  44. }
  45. },
  46. data() {
  47. return {
  48. bgColor: '#ffffff'
  49. }
  50. },
  51. computed: {
  52. isActive() {
  53. return this.value === this.activeValue;
  54. },
  55. switchStyle() {
  56. let style = {}
  57. // 这里需要加2,是为了腾出边框的距离,否则圆点node会和外边框紧贴在一起
  58. style.width = uni.$u.addUnit(this.size * 2 + 2)
  59. style.height = uni.$u.addUnit(Number(this.size) + 2)
  60. // style.borderColor = this.value ? 'rgba(0, 0, 0, 0)' : 'rgba(0, 0, 0, 0.12)'
  61. // 如果自定义了“非激活”演示,name边框颜色设置为透明(跟非激活颜色一致)
  62. // 这里不能简单的设置为非激活的颜色,否则打开状态时,会有边框,所以需要透明
  63. if (this.customInactiveColor) {
  64. style.borderColor = 'rgba(0, 0, 0, 0)'
  65. }
  66. style.backgroundColor = this.isActive ? this.activeColor : this.inactiveColor
  67. return style;
  68. },
  69. nodeStyle() {
  70. let style = {}
  71. // 如果自定义非激活颜色,将node圆点的尺寸减少两个像素,让其与外边框距离更大一点
  72. style.width = uni.$u.addUnit(this.size - this.space)
  73. style.height = uni.$u.addUnit(this.size - this.space)
  74. const translateX = this.isActive ? uni.$u.addUnit(this.space) : uni.$u.addUnit(this.size);
  75. style.transform = `translateX(-${translateX})`
  76. return style
  77. },
  78. bgStyle() {
  79. let style = {}
  80. // 这里配置一个多余的元素在HTML中,是为了让switch切换时,有更良好的背景色扩充体验(见实际效果)
  81. style.width = uni.$u.addUnit(Number(this.size) * 2 - this.size / 2)
  82. style.height = uni.$u.addUnit(this.size)
  83. style.backgroundColor = this.inactiveColor
  84. // 打开时,让此元素收缩,否则反之
  85. style.transform = `scale(${this.isActive ? 0 : 1})`
  86. return style
  87. },
  88. customInactiveColor() {
  89. // 之所以需要判断是否自定义了“非激活”颜色,是为了让node圆点离外边框更宽一点的距离
  90. return this.inactiveColor !== '#fff' && this.inactiveColor !== '#ffffff'
  91. }
  92. },
  93. methods: {
  94. clickHandler() {
  95. if (this.disabled) {
  96. uni.showModal({
  97. title: "提示",
  98. showCancel: false,
  99. confirmColor: '#F7790C',
  100. content: '同一时间有且只有一种价格显示哦',
  101. success: (res) => {
  102. if (res.confirm) {
  103. }
  104. }
  105. })
  106. return
  107. }
  108. if (!this.disabled && !this.loading) {
  109. const oldValue = this.isActive ? 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. opacity: 1;
  166. }
  167. }
  168. </style>