使用Vue.js實現Tab選項卡

 

 

 

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Tab選項卡</title>
        <script src="js/vue.js"></script>
        <style>
            *{
                margin: 0px;
                padding: 0px;
            }
            #tab{
                width:310px;
                margin: 20px auto;
                position: relative;
            }
            #tab ul li{
                width: 100px;
                height: 30px;
                border: 1px solid deeppink;
                float: left;
                list-style: none;
                text-align: center;
                line-height: 30px;
            }
            #tab ul li:first-child{
                border-right: none;
                border-radius: 10px 0px 0px 10px;
            }
            #tab ul li:last-child{
                border-left: none;
                border-radius: 0px 10px 10px 0px;
            }
            #tab ul .active{
                background-color:deeppink;
                color:white;
            }
            #tab div{
                width: 305px;
                position: absolute;
                top: 32px;
                display: none;
            }
            #tab div img{
                width: 305px;
                border-radius: 10px;
            }
            #tab div.current{
                display: block;
            }
            
        </style>
    </head>
    <body>
        <div id="tab">
            <ul>
                <li @mouseover="change(index)" :class="currentindex==index?'active':''":key="item.id"v-for="(item,index) in list">{{item.num}}</li>
            </ul>
            <div :class="currentindex==index?'current':''" v-for="(item,index) in list">
                <img :key="item.id"  v-bind:src="item.imgsrc"/>
            </div>
        </div>
        <script>
            var vm = new Vue({
                el:'#tab',
                data:{
                    currentindex:'0',//當前選項卡的索引
                    list:[{
                        id:'1',
                        num:'照片1',
                        imgsrc:'img/1.jpg'
                    },{
                        id:'2',
                        num:'照片2',
                        imgsrc:'img/2.jpg'
                    },{
                        id:'3',
                        num:'照片3',
                        imgsrc:'img/3.jpg'
                    }]
                },
                methods:{
                    change:function(index){
                        this.currentindex=index;
                    }
                }
            });
        </script>
    </body>
</html>
相關文章
相關標籤/搜索