vue 3学习记录之选项卡 tabs

html/vue

  <!-- 选项卡 -->
      <!-- <div v-bind:class="{ active: isActive }"></div> ; 
        //这里我们使用isActive 这个变量动态判断active是否显示到html -->
      <div class="mytab">
        <div class="tabs">
          <div
            type="button"
            v-for="(tab, index) in tabs"
            :key="index"
            @click="switchTab(index)"
            :class="{ active: currentTab === index }"
          >
            {{ tab.title }}
          </div>
        </div>
        <component :is="currentTabComponent" />
      </div>

js内容

/*采用vue选项式风格*/
import { ref } from 'vue';
import ComponentA from './views/book_1.vue';
import ComponentB from './views/book_2.vue';
import ComponentC from './views/book_3.vue';

let tabs = [
    { title: '史地理论', component: ComponentA },
    { title: '史地志', component: ComponentB },
    { title: '地方志', component: ComponentC },
]
// 当前选中的tab索引
let currentTab = ref(0);
// 根据当前索引获取对应的组件
// 切换Tab函数
function switchTab(tabIndex) {
    currentTab.value = tabIndex;
}
export default {
    data() {
        return {
            tabs,
            currentTab,
        };
    },
    methods: {
        switchTab,
    },
    computed: {
        currentTabComponent: () => { return tabs[currentTab.value].component; }
    }
};

css样式

.tabs{
    display: flex;
    justify-content: center;
    height: 60px;
    margin:auto;
    background-color: rgb(160, 241, 241);
}
.tabs div{
    position: relative;
    text-align: center;
    height: 100%;
    width: 7%;
    color: rgb(80, 93, 107);
    background-color: rgb(160, 241, 241);
    line-height: 350%;

}
.tabs .active {
    background-image: linear-gradient(to right, #a6c1ee, #7bd38e); /* 设置激活的Tab标题颜色 */
  }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

先生余枫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值