解决【Element-ui的Tabs页签组件在TabPane按钮宽度发生变化后,底部active-bar定位不准确】

20 篇文章 0 订阅

问题描述:

由于TabPane文字数量发生变化,TabPane宽度变化,被选中的按钮下方的指示线定位不准确,如下图:

 ​​​


解决后:

解决方案:

在组件中绑定ref:

<el-tabs ref="tabs"></el-tabs>

watch: {
    currentLang(){
        resetTabActivePosition(this.$refs.tabs.$el)
    }
},
method: {
    resetTabActivePosition($el) {
        this.$nextTick(() => {
            const activeEl = $el.querySelector('.el-tabs__item.is-active');
            const lineEl = $el.querySelector('.el-tabs__active-bar');
            const style = getComputedStyle(activeEl);
            const pl = style.paddingLeft.match(/\d+/)[0] * 1;
            const pr = style.paddingRight.match(/\d+/)[0] * 1;
            const w = style.width.match(/\d+/)[0] * 1;
            lineEl.style.transform = 'translateX(' + (activeEl.offsetLeft + pl) + 'px)';
            lineEl.style.width = (w - pl - pr)+'px';
        })
    }
}
Element UI 的分组组件通常指的是 `el-collapse` 或 `el-tabs` 这样的折叠面板或选项卡组件,它们默认并不包含上下按钮。如果你想在分组组件上添加自定义的上下导航按钮并带有边框,你需要进行一些定制和样式调整。 首先,你可以通过 Vue.js 的 `v-for` 指令创建额外的按钮,并绑定点击事件去切换内容。例如,对于 `el-tabs` 组件: ```html <template> <el-tabs v-model="activeName" @tab-click="handleTabClick"> <!-- 原始 tabs --> <el-tab-pane label="Tab 1" name="1">Tab 1 Content</el-tab-pane> <el-tab-pane label="Tab 2" name="2">Tab 2 Content</el-tab-pane> <!-- 添加的下一和上一按钮 --> <el-button type="primary" @click="prevTab" :disabled="isFirstPage">上一</el-button> <el-button type="primary" @click="nextTab" :disabled="isLastPage">下一</el-button> </el-tabs> </template> <script> export default { data() { return { activeName: '1', tabs: [ // 更多 tab 内容... ], isFirstPage: true, isLastPage: false, }; }, methods: { handleTabClick(tab) { this.activeName = tab.name; this.isFirstPage = (this.tabs.findIndex(t => t.name === this.activeName) === 0); this.isLastPage = (this.tabs.findIndex(t => t.name === this.activeName) === this.tabs.length - 1); }, prevTab() { if (this.isFirstPage) return; this.handleTabClick(this.tabs[this.tabs.findIndex(t => t.name !== this.activeName)]); }, nextTab() { if (this.isLastPage) return; const index = this.tabs.findIndex(t => t.name === this.activeName); this.handleTabClick(this.tabs[index + 1]); }, }, }; </script> <style scoped> .el-tabs__nav .custom-pagination { display: flex; justify-content: space-between; } .custom-pagination button { border: 1px solid #ccc; /* 添加边框 */ } </style> ``` 然后,在你的 CSS 中为这些自定义按钮添加适当的样式,如上面的 `custom-pagination` 类名所示。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__畫戟__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值