123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <template>
- <view
- class="u-switch"
- :class="[disabled && 'u-switch--disabled']"
- :style="[switchStyle, $u.addStyle(customStyle)]"
- @tap="clickHandler"
- >
- <view
- class="u-switch__bg"
- :style="[bgStyle]"
- >
- </view>
- <view
- class="u-switch__node"
- :class="[value && 'u-switch__node--on']"
- :style="[nodeStyle]"
- ref="u-switch__node"
- >
- <u-loading-icon
- :show="loading"
- mode="circle"
- timingFunction='linear'
- :color="value ? activeColor : '#AAABAD'"
- :size="size * 0.6"
- />
- </view>
- </view>
- </template>
- <script>
- import props from './props.js';
-
- export default {
- name: "u-switch",
- mixins: [uni.$u.mpMixin, uni.$u.mixin,props],
- watch: {
- value: {
- immediate: true,
- handler(n) {
- if(n !== this.inactiveValue && n !== this.activeValue) {
- uni.$u.error('v-model绑定的值必须为inactiveValue、activeValue二者之一')
- }
- }
- }
- },
- data() {
- return {
- bgColor: '#ffffff'
- }
- },
- computed: {
- isActive(){
- return this.value === this.activeValue;
- },
- switchStyle() {
- let style = {}
-
- style.width = uni.$u.addUnit(this.size * 2 + 2)
- style.height = uni.$u.addUnit(Number(this.size) + 2)
-
-
-
- if(this.customInactiveColor) {
- style.borderColor = 'rgba(0, 0, 0, 0)'
- }
- style.backgroundColor = this.isActive ? this.activeColor : this.inactiveColor
- return style;
- },
- nodeStyle() {
- let style = {}
-
- style.width = uni.$u.addUnit(this.size - this.space)
- style.height = uni.$u.addUnit(this.size - this.space)
- const translateX = this.isActive ? uni.$u.addUnit(this.space) : uni.$u.addUnit(this.size);
- style.transform = `translateX(-${translateX})`
- return style
- },
- bgStyle() {
- let style = {}
-
- style.width = uni.$u.addUnit(Number(this.size) * 2 - this.size / 2)
- style.height = uni.$u.addUnit(this.size)
- style.backgroundColor = this.inactiveColor
-
- style.transform = `scale(${this.isActive ? 0 : 1})`
- return style
- },
- customInactiveColor() {
-
- return this.inactiveColor !== '#fff' && this.inactiveColor !== '#ffffff'
- }
- },
- methods: {
- clickHandler() {
- if (!this.disabled && !this.loading) {
- const oldValue = this.isActive ? this.inactiveValue : this.activeValue
- if(!this.asyncChange) {
- this.$emit('input', oldValue)
- }
-
- this.$nextTick(() => {
- this.$emit('change', oldValue)
- })
- }
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- @import "../../libs/css/components.scss";
- .u-switch {
- @include flex(row);
-
- box-sizing: border-box;
-
- position: relative;
- background-color: #fff;
- border-width: 1px;
- border-radius: 100px;
- transition: background-color 0.4s;
- border-color: rgba(0, 0, 0, 0.12);
- border-style: solid;
- justify-content: flex-end;
- align-items: center;
-
-
- overflow: hidden;
- &__node {
- @include flex(row);
- align-items: center;
- justify-content: center;
- border-radius: 100px;
- background-color: #fff;
- border-radius: 100px;
- box-shadow: 1px 1px 1px 0 rgba(0, 0, 0, 0.25);
- transition-property: transform;
- transition-duration: 0.4s;
- transition-timing-function: cubic-bezier(0.3, 1.05, 0.4, 1.05);
- }
- &__bg {
- position: absolute;
- border-radius: 100px;
- background-color: #FFFFFF;
- transition-property: transform;
- transition-duration: 0.4s;
- border-top-left-radius: 0;
- border-bottom-left-radius: 0;
- transition-timing-function: ease;
- }
- &--disabled {
- opacity: 0.6;
- }
- }
- </style>
|