vue实现tab页

本文介绍如何使用Vue.js实现Tab切换效果,通过绑定和监听事件,动态显示对应的div内容。利用v-for指令遍历数据列表,根据点击的标题索引显示相应的div。

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

<!DOCTYPE html>
<html>
<head>
    <title>tab</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <script type="text/javascript" src='js/vue.js'></script>
    <script type="text/javascript" src='js/index.js'></script>
</head>
<!-- 
思想:
其实tab切换分两大块,上面的哪一行是一个ul,里面有四个li,下面的内容是四个div只不过只显示一个,其内容对应上面的li
现在要做的就是点击li时显示对应的div。所以当我们点击li时可以获取其index值,然后显示对应index的div。 -->

<body>
    <div id="my">
        <div class="myTab">
            <ul class="tab-tit">
                <!-- 遍历lists获取title的值以及index,点击时获取该index并将其传给num。 -->
                <li v-for="(v,i) in lists" @click="change(i)" :class="{'active':num==i}">
                    {{v.title}}
                </li>
            </ul>
            <div>
                <!-- 遍历lists获取content的值以及index,显示index==num的div,这个num对应的是title的index -->
                <div v-for="(v,i) in lists" v-show="num==i">
                      {{v.content}}
                </div>
            </div>
        </div>
    </div>

</body>

</html>
window.onload=function(){
    new Vue({
        el:'#my',//挂载元素
        data:{
            num:0,
            lists:[
                {title:'AAA',content:'aaa'},
                {title:'BBB',content:'bbb'},
                {title:'CCC',content:'ccc'},
                {title:'DDD',content:'ddd'}
            ]
        },
        methods:{//change方法,点击时将点击的title的index传给num,通过num来控制显示的cntent内容
            change:function(i){
                 this.num=i;
            }
        }

    });
}

实现Vuetab面切换,你可以按照以下步骤进行: 1. 在Vue中使用Vue Router来管理路由。首先需要在Vue项目中安装Vue Router,然后在main.js中引入并使用Vue Router。 2. 在Vue Router中设置多个路由,每个路由对应一个面。你可以将这些路由设置为子路由,这样每个路由都可以通过某个父路由来访问。 3. 在Vue中创建一个组件来显示tab导航栏,这个导航栏需要包含多个选项卡,每个选项卡对应一个路由。 4. 在Vue中创建多个组件,每个组件对应一个路由所对应的面。 5. 在Vue中通过路由跳转来实现面的切换。当你点击tab导航栏中的某个选项卡时,Vue Router会根据路由配置自动跳转到对应的面组件。 6. 可以通过一些样式效果来美化tab导航栏,比如选中的选项卡可以高亮显示。 下面是一个示例代码,用来实现Vuetab面切换: ``` // main.js import Vue from 'vue' import VueRouter from 'vue-router' import App from './App.vue' import Home from './components/Home.vue' import About from './components/About.vue' import Contact from './components/Contact.vue' Vue.use(VueRouter) const router = new VueRouter({ routes: [ { path: '/', component: Home }, { path: '/about', component: About }, { path: '/contact', component: Contact } ] }) new Vue({ router, render: h => h(App) }).$mount('#app') // App.vue <template> <div> <div class="tabs"> <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> <router-link to="/contact">Contact</router-link> </div> <router-view></router-view> </div> </template> // Home.vue <template> <div> <h1>Home Page</h1> <p>Welcome to the home page!</p> </div> </template> // About.vue <template> <div> <h1>About Page</h1> <p>Learn more about us!</p> </div> </template> // Contact.vue <template> <div> <h1>Contact Page</h1> <p>Get in touch with us!</p> </div> </template> // style.css .tabs { display: flex; justify-content: space-around; margin-bottom: 20px; } .tabs a { padding: 10px; border: 1px solid #ccc; } .tabs a.router-link-active { background-color: #ccc; } ``` 在上面的代码中,我们首先在main.js中引入并使用了Vue Router,并设置了三个路由:'/','/about'和'/contact'。然后在App.vue中创建了一个tab导航栏,包含了三个选项卡,每个选项卡对应一个路由。最后在Home.vue、About.vue和Contact.vue中分别定义了三个面组件。当你点击tab导航栏中的某个选项卡时,Vue Router会自动跳转到对应的面组件。 你可以根据自己的需要,对上面的示例代码进行修改和调整,来满足自己的实际需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值