vue.js开发之开关(switch)组件

     最近开发组件的时候,自定义开发了开关(switch)组件,现将代码整理如下,方便日后复用。

 

toggle-switch.vue  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<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: '关'
      }
    },
    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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<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>

  

 

posted @   小旭的blog  阅读(12634)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示