vue.js封裝switch開關(guān)組件的操作
我的項目本來用的element,但是switch開關(guān)不符合設(shè)計要求,于是自己封裝了一個switch組件,并且實現(xiàn)了switch開關(guān)的雙向數(shù)據(jù)綁定
<template> <label role="checkbox" :class="['switch', { toggled }]"> <input type="checkbox" class="switch-input" @change="toggle" /> <div class="switch-core" :style="{ backgroundColor: toggled ? colorChecked : colorUnchecked }" > <div class="switch-button" :style="{ transition: `transform ${speed}ms`, transform: toggled ? null : `translate3d(32px, 0px, 0px)` }" ></div> </div> <span class="switch-label label-right" v-if="toggled" v-html="labelChecked"> </span> <span class="switch-label label-left" v-html="labelUnchecked" v-else> </span> </label> </template>
<script> export default { name: "ToggleSwitch", data() { return { toggled: this.value }; }, props: { value: { type: Boolean, default: true }, speed: { type: Number, default: 100 }, labelChecked: { type: String, default: "啟用" }, labelUnchecked: { type: String, default: "停用" }, colorChecked: { type: String, default: "#11CED2" }, colorUnchecked: { type: String, default: "#E6EAF1" } }, watch: { value: function(val) { this.value = val; } }, methods: { toggle(event) { this.toggled = !this.toggled; this.$emit("update:value", this.toggled); this.$emit("change", event); } } }; </script>
<style lang="scss" scoped> .switch { display: inline-block; position: relative; overflow: hidden; vertical-align: middle; user-select: none; font-size: 10px; cursor: pointer; .switch-input { display: none; } .switch-label { position: absolute; top: 0; font-weight: 600; color: white; z-index: 2; &.label-left { left: 10px; line-height: 20px; border-top-left-radius: 2px; border-bottom-left-radius: 2px; color: #b5bdc8; } &.label-right { right: 10px; line-height: 20px; border-top-right-radius: 2px; border-bottom-right-radius: 2px; } } .switch-core { display: block; position: relative; box-sizing: border-box; outline: 0; margin: 0; transition: border-color 0.3s, background-color 0.3s; user-select: none; width: 46px; height: 20px; border-radius: 4px; line-height: 20px; .switch-button { width: 8px; height: 16px; display: block; position: absolute; overflow: hidden; top: 2; left: 2; z-index: 3; transform: translate3d(0, 0, 0); background-color: rgba(255, 255, 255, 1); border-radius: 4px; margin-top: 2px; margin-left: 2px; } } } </style>
調(diào)用開關(guān)組件
<toggle-switch v-model="value" :value.sync="value" @change="switchChange" > </toggle-switch>
組件實現(xiàn)了switch開關(guān)的雙向數(shù)據(jù)綁定,在父組件的methods中寫一個@change事件
switchChange() { console.log("switch值改變"); },
補充知識:vue 監(jiān)控el-switch控件值的變化
我要的效果是根據(jù)系統(tǒng)消息的推送范圍決定推送人標(biāo)簽的顯示,如下圖兩種情況:
——選擇全站推送
——選擇個人推送
——頁面定義的data對象
el-switch標(biāo)簽控件的代碼, v-model="entity.pushRange"綁定的是推送范圍字段
<el-form-item label="推送范圍:" :label-width="formLabelWidth" prop="pushRange"> <el-switch v-model="entity.pushRange" active-color="#13ce66" inactive-color="#ff4949" active-text="全站" inactive-text="個人" @change="parens2($event)"> </el-switch> </el-form-item>
下面的推送人id文本框,v-if=“FalgpushId”用來控制該文本框的顯示,等于false時隱藏,true時顯示(默認值為初始化時定義的FalgpushId = false,所以隱藏掉了)
<!-- 推送人id --> <el-form-item label="推送人ID:" :label-width="formLabelWidth" prop="pushId" v-if="FalgpushId"> <el-input v-model="entity.pushId" :disabled="disabled" clearable></el-input> </el-form-item>
methods中的方法,通過$event寫法來監(jiān)控該控件值的變化
methods:{ //該方法傳入推送范圍值,根據(jù)判斷,決定是否展示其下面的推送人ID文本框 parens2(value){ let self = this ; if(value == false){ //el-switch控件為 個人推送時,value為false self.FalgpushId = true; //推送人id文本框顯示 self.entity.pushRange = false; }else if(value == true){ //el-switch控件為 true 全站推送,value為true self.FalgpushId = false; //推送人id文本框隱藏 self.entity.pushRange = true; } }, }
以上這篇vue.js封裝switch開關(guān)組件的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue.js中使用echarts實現(xiàn)數(shù)據(jù)動態(tài)刷新功能
這篇文章主要介紹了vue.js中使用echarts實現(xiàn)數(shù)據(jù)動態(tài)刷新功能,需要的朋友可以參考下2019-04-04vue elementui表格獲取某行數(shù)據(jù)(slot-scope和selection-change方法使用)
這篇文章主要介紹了vue elementui表格獲取某行數(shù)據(jù)(slot-scope和selection-change方法使用),本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01Vue render渲染時間戳轉(zhuǎn)時間,時間轉(zhuǎn)時間戳及渲染進度條效果
這篇文章主要介紹了Vue render渲染時間戳轉(zhuǎn)時間,時間轉(zhuǎn)時間戳及渲染進度條效果,通過實例代碼相結(jié)合的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07