From d1448cb0ef10f358bb7bddb4e1ec268515e0b787 Mon Sep 17 00:00:00 2001 From: gaoluyang <2820782392@qq.com> Date: 星期二, 15 七月 2025 11:46:57 +0800 Subject: [PATCH] 项目初始化 --- uni_modules/uview-ui/components/u-switch/u-switch.vue | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 177 insertions(+), 0 deletions(-) diff --git a/uni_modules/uview-ui/components/u-switch/u-switch.vue b/uni_modules/uview-ui/components/u-switch/u-switch.vue new file mode 100644 index 0000000..6f8577b --- /dev/null +++ b/uni_modules/uview-ui/components/u-switch/u-switch.vue @@ -0,0 +1,177 @@ +<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'; + /** + * switch 寮�鍏抽�夋嫨鍣� + * @description 閫夋嫨寮�鍏充竴鑸敤浜庡彧鏈変袱涓�夋嫨锛屼笖鍙兘閫夊叾涓�鐨勫満鏅�� + * @tutorial https://www.uviewui.com/components/switch.html + * @property {Boolean} loading 鏄惁澶勪簬鍔犺浇涓紙榛樿 false 锛� + * @property {Boolean} disabled 鏄惁绂佺敤锛堥粯璁� false 锛� + * @property {String | Number} size 寮�鍏冲昂瀵革紝鍗曚綅px 锛堥粯璁� 25 锛� + * @property {String} activeColor 鎵撳紑鏃剁殑鑳屾櫙鑹� 锛堥粯璁� '#2979ff' 锛� + * @property {String} inactiveColor 鍏抽棴鏃剁殑鑳屾櫙鑹� 锛堥粯璁� '#ffffff' 锛� + * @property {Boolean | String | Number} value 閫氳繃v-model鍙屽悜缁戝畾鐨勫�� 锛堥粯璁� false 锛� + * @property {Boolean | String | Number} activeValue 鎵撳紑閫夋嫨鍣ㄦ椂閫氳繃change浜嬩欢鍙戝嚭鐨勫�� 锛堥粯璁� true 锛� + * @property {Boolean | String | Number} inactiveValue 鍏抽棴閫夋嫨鍣ㄦ椂閫氳繃change浜嬩欢鍙戝嚭鐨勫�� 锛堥粯璁� false 锛� + * @property {Boolean} asyncChange 鏄惁寮�鍚紓姝ュ彉鏇达紝寮�鍚悗闇�瑕佹墜鍔ㄦ帶鍒惰緭鍏ュ�� 锛堥粯璁� false 锛� + * @property {String | Number} space 鍦嗙偣涓庡杈规鐨勮窛绂� 锛堥粯璁� 0 锛� + * @property {Object} customStyle 瀹氫箟闇�瑕佺敤鍒扮殑澶栭儴鏍峰紡 + * + * @event {Function} change 鍦╯witch鎵撳紑鎴栧叧闂椂瑙﹀彂 + * @example <u-switch v-model="checked" active-color="red" inactive-color="#eee"></u-switch> + */ + 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銆乤ctiveValue浜岃�呬箣涓�') + } + } + } + }, + data() { + return { + bgColor: '#ffffff' + } + }, + computed: { + isActive(){ + return this.value === this.activeValue; + }, + switchStyle() { + let style = {} + // 杩欓噷闇�瑕佸姞2锛屾槸涓轰簡鑵惧嚭杈规鐨勮窛绂伙紝鍚﹀垯鍦嗙偣node浼氬拰澶栬竟妗嗙揣璐村湪涓�璧� + style.width = uni.$u.addUnit(this.size * 2 + 2) + style.height = uni.$u.addUnit(Number(this.size) + 2) + // style.borderColor = this.value ? 'rgba(0, 0, 0, 0)' : 'rgba(0, 0, 0, 0.12)' + // 濡傛灉鑷畾涔変簡鈥滈潪婵�娲烩�濇紨绀猴紝name杈规棰滆壊璁剧疆涓洪�忔槑(璺熼潪婵�娲婚鑹蹭竴鑷�) + // 杩欓噷涓嶈兘绠�鍗曠殑璁剧疆涓洪潪婵�娲荤殑棰滆壊锛屽惁鍒欐墦寮�鐘舵�佹椂锛屼細鏈夎竟妗嗭紝鎵�浠ラ渶瑕侀�忔槑 + if(this.customInactiveColor) { + style.borderColor = 'rgba(0, 0, 0, 0)' + } + style.backgroundColor = this.isActive ? this.activeColor : this.inactiveColor + return style; + }, + nodeStyle() { + let style = {} + // 濡傛灉鑷畾涔夐潪婵�娲婚鑹诧紝灏唍ode鍦嗙偣鐨勫昂瀵稿噺灏戜袱涓儚绱狅紝璁╁叾涓庡杈规璺濈鏇村ぇ涓�鐐� + 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 = {} + // 杩欓噷閰嶇疆涓�涓浣欑殑鍏冪礌鍦℉TML涓紝鏄负浜嗚switch鍒囨崲鏃讹紝鏈夋洿鑹ソ鐨勮儗鏅壊鎵╁厖浣撻獙(瑙佸疄闄呮晥鏋�) + 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() { + // 涔嬫墍浠ラ渶瑕佸垽鏂槸鍚﹁嚜瀹氫箟浜嗏�滈潪婵�娲烩�濋鑹诧紝鏄负浜嗚node鍦嗙偣绂诲杈规鏇村涓�鐐圭殑璺濈 + 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) + } + // 鏀惧埌涓嬩竴涓敓鍛藉懆鏈燂紝鍥犱负鍙屽悜缁戝畾鐨剉alue淇敼鐖剁粍浠剁姸鎬侀渶瑕佹椂闂达紝涓旀槸寮傛鐨� + this.$nextTick(() => { + this.$emit('change', oldValue) + }) + } + } + } + }; +</script> + +<style lang="scss" scoped> + @import "../../libs/css/components.scss"; + + .u-switch { + @include flex(row); + /* #ifndef APP-NVUE */ + box-sizing: border-box; + /* #endif */ + 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; + // 鐢变簬weex涓洪樋閲岄�楃潃鐜╃殑KPI椤圭洰锛屽鑷碽ug濂囧锛岃繖蹇呴』瑕佸啓杩欎竴琛岋紝 + // 鍚﹀垯鍦╥OS涓婏紝鐐瑰嚮椤甸潰浠绘剰鍦版柟锛岄兘浼氳Е鍙憇witch鐨勭偣鍑讳簨浠� + 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> -- Gitblit v1.9.3