vue3el-tabs水平居中下划线不变
时间: 2023-05-19 10:07:11 浏览: 127
手动动态改变el-tabs标签页顺序
对于这个问题,你可以参考以下代码实现:
<template>
<div class="tabs-container">
<div class="tabs-header">
<div class="tabs-header-item" v-for="(tab, index) in tabs" :key="index" :class="{ active: activeIndex === index }" @click="activeIndex = index">
{{ tab }}
</div>
<div class="tabs-header-underline" :style="{ width: underlineWidth + 'px', left: underlineLeft + 'px' }"></div>
</div>
<div class="tabs-content">
<slot></slot>
</div>
</div>
</template>
<script>
export default {
name: 'ElTabs',
props: {
tabs: {
type: Array,
default: () => []
}
},
data() {
return {
activeIndex: 0,
underlineLeft: 0,
underlineWidth: 0
}
},
mounted() {
this.setUnderline()
},
methods: {
setUnderline() {
const header = this.$refs.header
const activeItem = header.getElementsByClassName('active')[0]
this.underlineLeft = activeItem.offsetLeft
this.underlineWidth = activeItem.offsetWidth
}
},
watch: {
activeIndex() {
this.setUnderline()
}
}
}
</script>
<style scoped>
.tabs-container {
position: relative;
}
.tabs-header {
display: flex;
justify-content: center;
align-items: center;
}
.tabs-header-item {
padding: 10px 20px;
cursor: pointer;
}
.tabs-header-item.active {
font-weight: bold;
}
.tabs-header-underline {
position: absolute;
bottom: 0;
height: 2px;
background-color: #409EFF;
transition: all 0.3s ease-in-out;
}
</style>
这个组件可以实现一个基本的选项卡功能,其中下划线的位置和宽度会随着选项卡的切换而变化,从而实现水平居中的效果。
阅读全文