超简单vue开关组件switch的实现

因为太简单没什么好讲的,就直接上代码了,效果如图


e-switch
<template>
    <div :class="['e__switch',{'e_is-checked':value}]">
        <span class="e__switch-core" @click="toggle"></span>
    </div>
</template>

<script>
    export default {
        name: 'e-switch',
        props:['value'],
        methods:{
            toggle(){
                this.$emit('change',!this.value);
                this.$emit('input',!this.value);
            }
        }
    }
</script>

<style>
    .e__switch {
        display: inline-flex;
        align-items: center;
        position: relative;
        font-size: 14px;
        line-height: 20px;
        height: 20px;
        vertical-align: middle;
        cursor: pointer;
    }

    .e__switch>input {
        position: absolute;
        width: 0;
        height: 0;
        opacity: 0;
        margin: 0;
    }

    .e__switch-core {
        margin: 0;
        position: relative;
        width: 40px;
        height: 20px;
        border: 1px solid #DCDFE6;
        outline: 0;
        border-radius: 10px;
        box-sizing: border-box;
        background: #DCDFE6;
        transition: border-color .3s, background-color .3s;
        vertical-align: middle;
    }

    .e__switch-core::after {
        content: "";
        position: absolute;
        top: 1px;
        left: 1px;
        border-radius: 100%;
        transition: all .3s;
        width: 16px;
        height: 16px;
        background-color: #FFF;
    }
    
    .e__switch.e_is-checked .e__switch-core {
        border-color: #409EFF;
        background-color: #409EFF;
    }
    
    .e__switch.e_is-checked .e__switch-core::after {
        left: 100%;
        margin-left: -17px;
    }
</style>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容