vue.js實(shí)現(xiàn)開關(guān)(switch)組件實(shí)例代碼
最近開發(fā)組件的時候,自定義開發(fā)了開關(guān)(switch)組件,現(xiàn)將代碼整理如下,方便日后復(fù)用。

toggle-switch.vue
<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,
colorChecked: '#25b9e9',
colorUnchecked: '#db572e',
labelChecked: '開',
labelUnchecked: '關(guān)'
}
},
props: {
value: {
type: Boolean,
default: true
},
speed: {
type: Number,
default: 100
}
},
methods: {
toggle (event) {
this.toggled = !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;
}
&.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 .3s, background-color .3s;
user-select: none;
width: 64px;
height: 20px;
border-radius: 4px;
line-height: 20px;
.switch-button {
width: 32px;
height: 20px;
display: block;
position: absolute;
overflow: hidden;
top: 0;
left: 0;
z-index: 3;
transform: translate3d(0, 0, 0);
background-color: #ecf0f5;
}
}
}
</style>App.vue
<template>
<div id="app">
<div class="left">
<toggle-switch></toggle-switch>
</div>
<div class="main">
<router-view></router-view>
</div>
</div>
</template>
<script>
import ToggleSwitch from '@/components/toggle-switch'
export default {
name: 'app',
components: {
ToggleSwitch
}
}
</script>
<style>
#app {
font-family: 'Microsoft YaHei','Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: left;
color: #2c3e50;
height:100%;
}
.left {
margin: 50px 200px;
}
.main{
float:left;
width:95%;
background-color: #EFF2F7;
height:100%;
overflow: auto;
}
</style>到此這篇關(guān)于vue.js實(shí)現(xiàn)開關(guān)(switch)組件的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue微信分享 vue實(shí)現(xiàn)當(dāng)前頁面分享其他頁面
這篇文章主要為大家詳細(xì)介紹了vue微信分享功能,vue實(shí)現(xiàn)當(dāng)前頁面分享其他頁面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
vue?目錄樹的展開與關(guān)閉的實(shí)現(xiàn)
Vue作為一款流行的前端框架,提供了一種數(shù)據(jù)驅(qū)動的方式來實(shí)現(xiàn)目錄樹,本文主要介紹了vue?目錄樹的展開與關(guān)閉的實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-11-11
利用vue3+threejs仿iView官網(wǎng)大波浪特效實(shí)例
最近好幾個vue項目都是用ivew作為UI框架,所以下面這篇文章主要給大家介紹了關(guān)于如何利用vue3?+?threejs仿iView官網(wǎng)大波浪特效的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
el-menu動態(tài)加載路由的實(shí)現(xiàn)
本文主要介紹了el-menu動態(tài)加載路由的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
關(guān)于Vue父子組件傳參和回調(diào)函數(shù)的使用
這篇文章主要介紹了關(guān)于Vue父子組件傳參和回調(diào)函數(shù)的使用,我們將某段代碼封裝成一個組件,而這個組件又在另一個組件中引入,而引入該封裝的組件的文件叫做父組件,被引入的組件叫做子組件,需要的朋友可以參考下2023-05-05

