天天看點

vue tab切換

<!-- 互動的邏輯

1==》如何實作 點選目前的那個菜單 給被點選的元素添加樣似

@click="getclcik(index)" 記錄目前被點選的元素的下标

:class="{active:cur==index} 如果比對成功,則給這個元素添加一個類active

2==》如何隻顯示 點選下标與之對應的内容

v-show="cur==index" 對應比對

<style>
            ul li {
                margin: 0;
                padding: 0;
                list-style: none;
            }

            #app {
                width: 600px;
                height: 400px;
                margin: 0 auto;
                border: 1px solid #ccc;
            }

            .tab-tilte {
                width: 90%;
            }

            .tab-tilte li {
                float: left;
                width: 25%;
                padding: 10px 0;
                text-align: center;
                background-color: #f4f4f4;
                cursor: pointer;
            }

            /* 點選對應的标題添加對應的背景顔色 */
            .tab-tilte .active {
                background-color: #09f;
                color: #fff;
            }

            .tab-content div {
                float: left;
                width: 25%;
                line-height: 100px;
                text-align: center;
            }
        </style>      
for="(title,index) in tabTitle" @click="getclcik(index)" :class="{active:cur==index}">{{title}}</li>
            </ul>
            
            <div class="tab-content">
                <div v-for="(m,index) in tabMain" v-show="cur==index">{{m}}</div>
            </div>
        </div>      
var app = new Vue({
            el: '#app',
            data: {
                tabTitle: ['标題一', '标題二', '标題三', '标題四'],
                tabMain: ['内容一', '内容二', '内容三', '内容四'],
                cur: 0 //預設選中第一個tab
            },
            methods:{
              getclcik(value){
                 this.cur=value; 
              }    
            }
        })      
vue tab切換

遇見問題,這是你成長的機會,如果你能夠解決,這就是收獲。

繼續閱讀