ElementUI中switch开关的使用--关于如何把文字显示在按钮上

这里写自定义目录标题

  • 有关于element中switch的用法
    • 实现的效果图
    • 具体实现步骤:

有关于element中switch的用法

el-switch的详细用法

实现的效果图

ElementUI中switch开关的使用--关于如何把文字显示在按钮上_第1张图片

具体实现步骤:

(1)添加html代码的显示,此处写了class="switch"类名,是为了避免造成全局样式的污染。

 <el-table-column label="上下架" align="center">
     <template slot-scope="scope">
         <el-switch
             class="switch"
             v-model="scope.row.status"
             :active-value="0"
             :inactive-value="1"
             active-text="上架"
             inactive-text="下架"
         >
         </el-switch>
     </template>
 </el-table-column>

(2)修改el-switch的默认样式
我的样式是写在全局样式文件main.css中的,如果是想在el-switch所在文件中去修改样式的话,一定要注意style中不能有scoped属性,否则写的样式可能会不生效。但是如果去掉scoped的话,样式也会作用于全局,有可能会造成全局污染,所以建议在样式外面加一个全局类名。
小知识点:在使用vue框架的时候,给style标签添加scoped属性,表示该style标签所包含的样式仅仅作用于当前的vue组件,不会产生样式全局污染的情况,一个应用中所有vue组件都加上该属性,则实现了样式的模块化

/* switch按钮样式 */
.switch .el-switch__label {
     
    position: absolute;
    display: none;
    color: #fff !important;
}
/*打开时文字位置设置*/
.switch .el-switch__label--right {
     
    z-index: 1;
}
/* 调整打开时文字的显示位子 */
.switch .el-switch__label--right span{
     
    margin-right: 9px;
}
/*关闭时文字位置设置*/
.switch .el-switch__label--left {
     
    z-index: 1;
}
/* 调整关闭时文字的显示位子 */
.switch .el-switch__label--left span{
     
    margin-left: 9px;
}
/*显示文字*/
.switch .el-switch__label.is-active {
     
    display: block;
}
/* 调整按钮的宽度 */
.switch.el-switch .el-switch__core,
.el-switch .el-switch__label {
     
     width: 70px !important;
     margin: 0;
}

你可能感兴趣的:(vue+element,vue)