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

在真实项目中的需求需要实现的效果图:

![在这里插入图片描述](https://img-blog.csdnimg.cn/97a0aad6cd754cc297ec54fa427294d7.png#pic_center)

具体实现步骤:

(1) vue代码块,此处写了class="switchClass"类名
 		<el-table-column prop="status" label="状态" align="center">
	            <template slot-scope="scope">
	              <el-switch
	                v-model="scope.row.status"
	                class="switchClass"
	                :active-value="1"
	                :inactive-value="0"
	                :value="scope.row.status"
	                active-text="ON"
	                inactive-text="OFF"
	                @change="changeSwitch(scope.row)"
	              />
	              <span class="switch-text" :style="{[scope.row.status?'left':'right']:'26px',color:scope.row.status?'#ff9000':'#1D2129'}">{{ scope.row.status?'开':'关' }}</span>
	            </template>
        </el-table-column>
(2)修改el-switch的默认样式:

注意:一定要注意style中不能有scoped属性,否则写的样式可能会不生效。

		  /* switch按钮样式 */
		.switchClass .el-switch__label {
		    position: absolute;
		    display: none;
		    color: #fff !important;
		}
		/*打开时文字位置设置*/
		.switchClass .el-switch__label--right {
		    z-index: 1;
		}
		/* 调整打开时文字的显示位子 */
		.switchClass .el-switch__label--right span{
		    margin-right: 13px;
		    font-size: 12px;
		
		}
		/*关闭时文字位置设置*/
		.switchClass .el-switch__label--left {
		    z-index: 1;
		}
		/* 调整关闭时文字的显示位子 */
		.switchClass .el-switch__label--left span{
		    margin-left: 17px;
		    font-size: 12px;
		}
		/*显示文字*/
		.switchClass .el-switch__label.is-active {
		    display: block;
		}
		/* 调整按钮的宽度 */
		.switchClass.el-switch .el-switch__core,
		.el-switch .el-switch__label {
		     width: 42px !important;
		     margin: 0;
		}
		
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值