交换element-ui中$confirm弹出框的确定和取消按钮位置

本文介绍如何通过自定义CSS样式调整Element-UI MessageBox组件中的按钮布局,使其符合特定的设计要求,即确认按钮在左,取消按钮在右,并保持居中对齐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用element-ui的MessageBox组件时,用到了$confirm弹出框,它的默认样式是这样子的:
在这里插入图片描述
在项目开发中,为了保持风格的统一,UI给的切图都是确定在左,取消在右,且居中显示,类似如下图的效果:
在这里插入图片描述
看了下组件的Options参数,无法满足需求,于是琢磨着直接修改样式,研究后最终实现_
话不多说,直接上代码:

.el-message-box__btns {
    text-align: center;
    // 交换 确定 和 取消 按钮的位置
    .el-button:nth-child(1) {
        float: right;
        margin-right: 20%;
    }
    .el-button:nth-child(2) {
        float: left;
        margin-left: 20%;
    }
}

只需修改css样式,js调用的地方还是和以前保持一样,就能实现想要的效果啦,完美~

this.$confirm('此操作将永久删除该文件, 是否继续?', '删除');
Element UI的`this.$confirm`组件中,`message`属性用于设置确认消息的内容,通常是一个提示用户需要确认的操作的文字描述。默认情况下,这个消息会显示在上方,而两个操作按钮(通常是"确定""取消")会放在下方。 如果你想改变它们的位置,让按钮在上、消息在下,你可以通过自定义插槽(slot)来实现。在Vue模板中,可以在`.el-dialog__footer`这一部分添加你的自定义内容,将按钮替换到`.dialog-confirm__buttons`里面,并将确认消息插入到其他地方,例如`.dialog-confirm__message`。这里是一个简单的例子: ```html <template> <div> <el-dialog :visible.sync="dialogVisible" class="custom-confirm-dialog" > <div slot="footer"> <span class="custom-message">{{ message }}</span> <el-button @click="handleCancel">取消</el-button> <el-button type="primary" @click="handleConfirm">确定</el-button> </div> </el-dialog> </div> </template> <script> export default { data() { return { dialogVisible: false, message: '这是一条确认信息', }; }, methods: { handleConfirm() { // 确定操作 }, handleCancel() { // 取消操作 }, }, }; </script> <style scoped> .custom-confirm-dialog .dialog-confirm__message { /* 自定义样式 */ display: flex; align-items: center; justify-content: center; } .custom-confirm-dialog .custom-message { /* 将消息移动到原来按钮位置 */ position: absolute; bottom: 0; } </style> ``` 请注意,你需要根据实际的需求Element UI的版本调整CSS样式。如果Element UI提供官方支持的自定义布局选项,建议查阅其文档。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值