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

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

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(34px, 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: '#f55318',
	        colorUnchecked: '#25b9e9',
	        labelChecked: '开',
	        labelUnchecked: '关'
	      }
	    },
	    props: {
	      value: {
	        type: Boolean,
	        default: true
	      },
	      speed: {
	        type: Number,
	        default: 100
	      }
	    },
	    methods: {
	      toggle (event) {
	        this.toggled = !this.toggled
	        this.$emit('change', this.toggled)
	      }
	    }
	  }
</script>

<style lang="less" scoped>
  .switch {
  	float: right;
    display: inline-block;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    user-select: none;
    font-size: 10px;
    cursor: pointer;
 	border-radius: 15px;
 	margin-top: 10px;
 	
    .switch-input {
      display: none;
    }
 
    .switch-label {
      position: absolute;
      top: 0;
      font-weight: 600;
      color: white;
 
      z-index: 2;
 
      &.label-left {
        left: 10px;
        line-height: 25px;
        border-top-left-radius: 2px;
        border-bottom-left-radius:2px;
      }
 
      &.label-right {
        right: 10px;
        line-height: 25px;
        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: 70px;
      height: 25px;
      line-height: 25px;
 	  border-radius: 15px;
 	  
      .switch-button {
        width: 35px;
        height: 25px;
        display: block;
        position: absolute;
        overflow: hidden;
        border-radius: 15px;
        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 @change="handleMessage"></toggle-switch>
    </div>
  </div>
</template>
<script>
	import ToggleSwitch from '@/components/Switch'
	export default {
		data() {
			return {
			}
		},
		components: {
			NavHeader,
			ToggleSwitch
		},
		mounted: function() {
			
		},
		methods: {
			//开关键
			handleMessage(onoff){
				console.log(onoff)
			}
		}
	}
</script>

猜你喜欢

转载自blog.csdn.net/qq_42396791/article/details/85598811