From f26f29d84e0a68831a6af14dab3eec5500496d2e Mon Sep 17 00:00:00 2001 From: spring <2396852758@qq.com> Date: 星期三, 28 五月 2025 16:48:52 +0800 Subject: [PATCH] 初始化项目 --- uview-ui/components/u-switch/u-switch.vue | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 163 insertions(+), 0 deletions(-) diff --git a/uview-ui/components/u-switch/u-switch.vue b/uview-ui/components/u-switch/u-switch.vue new file mode 100644 index 0000000..6cb58d3 --- /dev/null +++ b/uview-ui/components/u-switch/u-switch.vue @@ -0,0 +1,163 @@ +<template> + <view class="u-switch" :class="[value == true ? 'u-switch--on' : '', disabled ? 'u-switch--disabled' : '']" @tap="onClick" + :style="[switchStyle]"> + <view class="u-switch__node node-class" :style="{ + width: $u.addUnit(this.size), + height: $u.addUnit(this.size) + }"> + <u-loading :show="loading" class="u-switch__loading" :size="size * 0.6" :color="loadingColor" /> + </view> + </view> +</template> + +<script> + /** + * switch 寮�鍏抽�夋嫨鍣� + * @description 閫夋嫨寮�鍏充竴鑸敤浜庡彧鏈変袱涓�夋嫨锛屼笖鍙兘閫夊叾涓�鐨勫満鏅�� + * @tutorial https://www.uviewui.com/components/switch.html + * @property {Boolean} loading 鏄惁澶勪簬鍔犺浇涓紙榛樿false锛� + * @property {Boolean} disabled 鏄惁绂佺敤锛堥粯璁alse锛� + * @property {String Number} size 寮�鍏冲昂瀵革紝鍗曚綅rpx锛堥粯璁�50锛� + * @property {String} active-color 鎵撳紑鏃剁殑鑳屾櫙鑹诧紙榛樿#497bff锛� + * @property {Boolean} inactive-color 鍏抽棴鏃剁殑鑳屾櫙鑹诧紙榛樿#ffffff锛� + * @property {Boolean | Number | String} active-value 鎵撳紑閫夋嫨鍣ㄦ椂閫氳繃change浜嬩欢鍙戝嚭鐨勫�硷紙榛樿true锛� + * @property {Boolean | Number | String} inactive-value 鍏抽棴閫夋嫨鍣ㄦ椂閫氳繃change浜嬩欢鍙戝嚭鐨勫�硷紙榛樿false锛� + * @event {Function} change 鍦╯witch鎵撳紑鎴栧叧闂椂瑙﹀彂 + * @example <u-switch v-model="checked" active-color="red" inactive-color="#eee"></u-switch> + */ + export default { + name: "u-switch", + props: { + // 鏄惁涓哄姞杞戒腑鐘舵�� + loading: { + type: Boolean, + default: false + }, + // 鏄惁涓虹鐢ㄨ濉� + disabled: { + type: Boolean, + default: false + }, + // 寮�鍏冲昂瀵革紝鍗曚綅rpx + size: { + type: [Number, String], + default: 50 + }, + // 鎵撳紑鏃剁殑鑳屾櫙棰滆壊 + activeColor: { + type: String, + default: '#497bff' + }, + // 鍏抽棴鏃剁殑鑳屾櫙棰滆壊 + inactiveColor: { + type: String, + default: '#ffffff' + }, + // 閫氳繃v-model鍙屽悜缁戝畾鐨勫�� + value: { + type: Boolean, + default: false + }, + // 鏄惁浣挎墜鏈哄彂鐢熺煭淇冮渿鍔紝鐩墠鍙湪iOS鐨勫井淇″皬绋嬪簭鏈夋晥(2020-05-06) + vibrateShort: { + type: Boolean, + default: false + }, + // 鎵撳紑閫夋嫨鍣ㄦ椂鐨勫�� + activeValue: { + type: [Number, String, Boolean], + default: true + }, + // 鍏抽棴閫夋嫨鍣ㄦ椂鐨勫�� + inactiveValue: { + type: [Number, String, Boolean], + default: false + }, + }, + data() { + return { + + } + }, + computed: { + switchStyle() { + let style = {}; + style.fontSize = this.size + 'rpx'; + style.backgroundColor = this.value ? this.activeColor : this.inactiveColor; + return style; + }, + loadingColor() { + return this.value ? this.activeColor : null; + } + }, + methods: { + onClick() { + if (!this.disabled && !this.loading) { + // 浣挎墜鏈轰骇鐢熺煭淇冮渿鍔紝寰俊灏忕▼搴忔湁鏁堬紝APP(HX 2.6.8)鍜孒5鏃犳晥 + if(this.vibrateShort) uni.vibrateShort(); + this.$emit('input', !this.value); + // 鏀惧埌涓嬩竴涓敓鍛藉懆鏈燂紝鍥犱负鍙屽悜缁戝畾鐨剉alue淇敼鐖剁粍浠剁姸鎬侀渶瑕佹椂闂达紝涓旀槸寮傛鐨� + this.$nextTick(() => { + this.$emit('change', this.value ? this.activeValue : this.inactiveValue); + }) + } + } + } + }; +</script> + +<style lang="scss" scoped> + @import "../../libs/css/style.components.scss"; + + .u-switch { + position: relative; + /* #ifndef APP-NVUE */ + display: inline-block; + /* #endif */ + box-sizing: initial; + width: 2em; + height: 1em; + background-color: #fff; + border: 1px solid rgba(0, 0, 0, 0.1); + border-radius: 1em; + transition: background-color 0.3s; + font-size: 50rpx; + } + + .u-switch__node { + @include vue-flex; + align-items: center; + justify-content: center; + position: absolute; + top: 0; + left: 0; + border-radius: 100%; + z-index: 1; + background-color: #fff; + background-color: #fff; + box-shadow: 0 3px 1px 0 rgba(0, 0, 0, 0.05), 0 2px 2px 0 rgba(0, 0, 0, 0.1), 0 3px 3px 0 rgba(0, 0, 0, 0.05); + box-shadow: 0 3px 1px 0 rgba(0, 0, 0, 0.05), 0 2px 2px 0 rgba(0, 0, 0, 0.1), 0 3px 3px 0 rgba(0, 0, 0, 0.05); + transition: transform 0.3s cubic-bezier(0.3, 1.05, 0.4, 1.05); + transition: transform 0.3s cubic-bezier(0.3, 1.05, 0.4, 1.05), -webkit-transform 0.3s cubic-bezier(0.3, 1.05, 0.4, 1.05); + transition: transform cubic-bezier(0.3, 1.05, 0.4, 1.05); + transition: transform 0.3s cubic-bezier(0.3, 1.05, 0.4, 1.05) + } + + .u-switch__loading { + @include vue-flex; + align-items: center; + justify-content: center; + } + + .u-switch--on { + background-color: #1989fa; + } + + .u-switch--on .u-switch__node { + transform: translateX(100%); + } + + .u-switch--disabled { + opacity: 0.4; + } +</style> -- Gitblit v1.9.3