vue.js實(shí)現(xiàn)開(kāi)關(guān)(switch)組件實(shí)例代碼
最近開(kāi)發(fā)組件的時(shí)候,自定義開(kāi)發(fā)了開(kāi)關(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: '開(kāi)', 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)開(kāi)關(guān)(switch)組件的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue如何點(diǎn)擊多個(gè)tab標(biāo)簽打開(kāi)關(guān)閉多個(gè)頁(yè)面
- vue自定義開(kāi)關(guān)組件使用詳解
- vue實(shí)現(xiàn)移動(dòng)端的開(kāi)關(guān)按鈕
- vue.js封裝switch開(kāi)關(guān)組件的操作
- Vue實(shí)現(xiàn)開(kāi)關(guān)按鈕拖拽效果
- vue自定義switch開(kāi)關(guān)組件,實(shí)現(xiàn)樣式可自行更改
- vue中el-tree結(jié)合el-switch實(shí)現(xiàn)開(kāi)關(guān)狀態(tài)切換
相關(guān)文章
vue微信分享 vue實(shí)現(xiàn)當(dāng)前頁(yè)面分享其他頁(yè)面
這篇文章主要為大家詳細(xì)介紹了vue微信分享功能,vue實(shí)現(xiàn)當(dāng)前頁(yè)面分享其他頁(yè)面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12vue后臺(tái)管理之動(dòng)態(tài)加載路由的方法
這篇文章主要介紹了vue后臺(tái)管理之動(dòng)態(tài)加載路由的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08vue?目錄樹(shù)的展開(kāi)與關(guān)閉的實(shí)現(xiàn)
Vue作為一款流行的前端框架,提供了一種數(shù)據(jù)驅(qū)動(dòng)的方式來(lái)實(shí)現(xiàn)目錄樹(shù),本文主要介紹了vue?目錄樹(shù)的展開(kāi)與關(guān)閉的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11利用vue3+threejs仿iView官網(wǎng)大波浪特效實(shí)例
最近好幾個(gè)vue項(xiàng)目都是用ivew作為UI框架,所以下面這篇文章主要給大家介紹了關(guān)于如何利用vue3?+?threejs仿iView官網(wǎng)大波浪特效的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12el-menu動(dòng)態(tài)加載路由的實(shí)現(xiàn)
本文主要介紹了el-menu動(dòng)態(tài)加載路由的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04關(guān)于Vue父子組件傳參和回調(diào)函數(shù)的使用
這篇文章主要介紹了關(guān)于Vue父子組件傳參和回調(diào)函數(shù)的使用,我們將某段代碼封裝成一個(gè)組件,而這個(gè)組件又在另一個(gè)組件中引入,而引入該封裝的組件的文件叫做父組件,被引入的組件叫做子組件,需要的朋友可以參考下2023-05-05