vue小案例之tab

效果:
在这里插入图片描述

主要就是利用v-for循环list里面的元素,然后渲染标题和图片
再判断currentIndex是否和当前遍历元素的索引index相等,相等就显示

通过事件绑定来动态根据鼠标点击的元素修改currentIndex

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>
        ul {
            overflow: hidden;
            padding: 0;
            margin: 0;
        }

        ul li {
            box-sizing: border-box;
            padding: 0;
            float: left;
            width: 100px;
            height: 45px;
            line-height: 45px;
            list-style: none;
            text-align: center;
            border-top: 1px solid blue;
            border-right: 1px solid blue;
            cursor: pointer;
        }

        .tab ul li:first-child {
            border-left: 1px solid blue;
        }

        .tab ul li.active {
            background-color: coral;
        }

        .tab div {
            box-sizing: border-box;
            width: 300px;
            height: 448px;
            display: none;
            text-align: center;
            line-height: 300px;
            border: 1px solid blue;
            border-top: 0px;
        }

        .tab div.current {
            display: block;
        }

        img {
            width: 298px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="tab">
            <ul>
                <li v-bind:class="currentIndex==index?'active':''" :key="item.id" v-for="(item,index) in list" v-on:click="handleClick(index)">{{item.title}}</li>
            </ul>
            <div v-bind:class="currentIndex==index?'current':''" :key="item.id" v-for="(item,index) in list">
                <img v-bind:src="item.src">
            </div>
        </div>

    </div>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                currentIndex:0,
                list: [{
                    id: 1,
                    title: 'Super Girl',
                    src: 'https://pic.yefu365.com/uploads/allimg/201712/90b8fab56ebc549c.jpg'
                }, {
                    id: 2,
                    title: 'High Castle',
                    src: 'https://pic.yefu365.com/uploads/allimg/201806/53a623f7d177f6b7.jpg'
                }, {
                    id: 3,
                    title: 'Mom',
                    src: 'https://pic.yefu365.com/uploads/allimg/171101/9e09d3953a34f377.jpg'
                }]
            },
            methods: {
                handleClick:function(id){
                    this.currentIndex = id
                }
            }
        });
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值