zoukankan      html  css  js  c++  java
  • vue简单的tab切换

    在使用vue的时候经常会用到tab切换,demo如下

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <script src="https://unpkg.com/vue/dist/vue.js"></script>
    </head>
    <style>
        .tab_box{
            display: flex;
            margin: 0 auto;
             500px;
        }
        .tab{
             100px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            color: #ffffff;
            background: #000000;
        }
        .tab_content{
            margin: 0 auto;
             500px;
            height: 300px;
            text-align: center;
            line-height: 300px;
            background: yellow;
            color: #000000;
            overflow: hidden;
        }
        .tab_content_item{
            height: 100%;
             100%;
        }
        .active{
            background: red;
            color: #000000;
        }
    </style>
    <body>
        <div id="app">
            <div class="tab_box">
                <!-- 循环tab头 (item,index)item为变量名 index为下标  @click="tabClick(index)" 设置点击事件把下标传进来 因为唯一可控的就是下标也是惟一的  :class = "num==index? 'active':''"  三元表达式满足条件就添加这个类名不满足就不添加-->
                <div v-for ="(item,index) in list" class="tab" @click="tabClick(index)" :class = "num==index? 'active':''">
                    {{item.name}}
                </div>
            </div>
            <div class="tab_content">
                    <!-- v-if = "num==index"  这是tabs身 num为0 初始化的定义的 就是让第一个显示其他的删除 当然这里也可以用v-show 效果是一样的但是 css 需要处理因为v-show 他是占位置的   -->
                <div class="tab_content_item" v-for ="(item,index) in list" v-if = "num==index">
                    我今年{{item.age}}兴趣是{{item.hobby}}
                </div>
            </div>
        </div>
    </body>
        <script>
            new Vue({  
                el: '#app',  
                data: {
                    //数据列表这只写了一个,根据需求tab头和tab身可以是两个对象,原理一样
                    list : [
                        {name:'小明',age:23,hobby:'打架'},
                        {name:'小强',age:25,hobby:'打篮球'},
                        {name:'小鹏',age:23,hobby:'看漫画'},
                        {name:'小李',age:23,hobby:'睡觉'},
                        {name:'小兰',age:23,hobby:'臭美'}
                    ],
                    //定义初始下标即页面初始化的要展示的样式
                    num:0
                },
                methods:{
                    //设置点击事件接收 index
                    tabClick:function(index){
                        console.log(index)
                        //m每次点击的时候重新赋值num 
                        this.num = index
                    }
                }  
            })
        </script>
    </html>
    效果如下
    如果实在不明白可以复制代码运行一下,动手才是硬道理
  • 相关阅读:
    Django继承user类来定制自己的user类
    pycharm下载 安装 使用
    360 so动态脱壳
    爱加密so保护简单脱壳测试
    菜鸟 学注册机编写之 “sha1”
    海海DRM视频保护解密流程分析
    通过CreateThreadpoolWait执行Shellcode
    .net学习--Fastjson反序列漏洞
    .net JavaScriptSerializer反序列化漏洞
    java web安全(自定义Classloader)--服务器端动态解析二进制class文件
  • 原文地址:https://www.cnblogs.com/qq976864507/p/11803755.html
Copyright © 2011-2022 走看看