vue自定義switch開關(guān)組件,實(shí)現(xiàn)樣式可自行更改
用法:
import switchc from './public/switch' <switchc v-model="value1" text="on|off"></switchc>
屬性
text 非必填,類型為string,要求格式為“on|off” ,以 | 分隔
事件
change
html部分:
<template> <div> <span class="weui-switch" :class="{'weui-switch-on' : isChecked}" :value="value" @click="toggle" style="position:relative"> <div v-if="isChecked && direction.length > 0" style="width:100%;height:100%;position:absolute;padding:0 5px;line-height:20px;color:#FFF;user-select:none"> {{direction[0]}} </div> <div v-if="!isChecked && direction.length > 0" style="width:100%;height:100%;position:absolute;padding:0 5px;right:2px;line-height:22px;color:#7A7A7A;text-align:right;user-select:none"> {{direction[1]}} </div> </span> </div> </template>
js部分:
<script> export default { name: 'switchComponent', props: { value: { type: Boolean, default: true }, text: { type: String, default: '' } }, data () { return { isChecked: this.value } }, computed: { direction () { if (this.text) { return this.text.split('|') } else { return [] } } }, watch: { value (val) { this.isChecked = val }, isChecked(val) { this.$emit('change', val); } }, methods: { toggle() { this.isChecked = !this.isChecked; } } } </script>
style部分:
<style> .weui-switch { display: block; position: relative; width: 52px; height: 24px; border: 1px solid #DFDFDF; outline: 0; border-radius: 16px; box-sizing: border-box; background-color: #DFDFDF; transition: background-color 0.1s, border 0.1s; cursor: pointer; } .weui-switch:before { content: " "; position: absolute; top: 0; left: 0; width: 50px; height: 22px; border-radius: 15px; background-color: #FDFDFD; transition: transform 0.35s cubic-bezier(0.45, 1, 0.4, 1); } .weui-switch:after { content: " "; position: absolute; top: 0; left: 0; width: 22px; height: 22px; border-radius: 15px; background-color: #FFFFFF; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4); transition: transform 0.35s cubic-bezier(0.4, 0.4, 0.25, 1.35); } .weui-switch-on { border-color: #6F6F6F; background-color: #1AAD19; } .weui-switch-on:before { border-color: #1AAD19; background-color: #409eff; } .weui-switch-on:after { transform: translateX(28px); } </style>
以上這篇vue自定義switch開關(guān)組件,實(shí)現(xiàn)樣式可自行更改就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
elementui源碼學(xué)習(xí)仿寫一個(gè)el-tooltip示例
本篇文章記錄仿寫一個(gè)el-tooltip組件細(xì)節(jié),從而有助于大家更好理解餓了么ui對(duì)應(yīng)組件具體工作細(xì)節(jié),本文是elementui源碼學(xué)習(xí)仿寫系列的又一篇文章,后續(xù)空閑了會(huì)不斷更新并仿寫其他組件2023-07-07vue實(shí)現(xiàn)點(diǎn)擊追加選中樣式效果
今天小編就為大家分享一篇vue實(shí)現(xiàn)點(diǎn)擊追加選中樣式效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11vue實(shí)現(xiàn)簡(jiǎn)易的雙向數(shù)據(jù)綁定
這篇文章主要介紹了vue如何實(shí)現(xiàn)簡(jiǎn)易的雙向數(shù)據(jù)綁定,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12electron-vue?運(yùn)行報(bào)錯(cuò)?Object.fromEntries?is?not?a?function
Object.fromEntries()?是?ECMAScript?2019?新增的一個(gè)靜態(tài)方法,用于將鍵值對(duì)列表(如數(shù)組)轉(zhuǎn)換為對(duì)象,如果在當(dāng)前環(huán)境中不支持該方法,可以使用?polyfill?來(lái)提供類似功能,接下來(lái)通過(guò)本文介紹electron-vue?運(yùn)行報(bào)錯(cuò)?Object.fromEntries?is?not?a?function的解決方案2023-05-05解決vue偵聽器watch,調(diào)用this時(shí)出現(xiàn)undefined的問(wèn)題
這篇文章主要介紹了解決vue偵聽器watch,調(diào)用this時(shí)出現(xiàn)undefined的問(wèn)題,具有很好的參考2020-10-10vue+elementUi實(shí)現(xiàn)點(diǎn)擊地圖自動(dòng)填充經(jīng)緯度以及地點(diǎn)
這篇文章主要為大家詳細(xì)介紹了vue+elementUi實(shí)現(xiàn)點(diǎn)擊地圖自動(dòng)填充經(jīng)緯度以及地點(diǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07