如何使用Vue寫一個簡單的選項卡功能


<!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>
    <style>
    
        .btnAtive{
            background: red;
            color: #fff;
        }
        .container{
            width:200px;
            height: 200px;
            overflow: hidden;
            background: #ccc;
        }

        .container>div{
            width: 200px;
            height: 200px;
        }
    </style>
</head>
<body>
    <div id="app">
        <button v-for="(item,index) in btns"
            :class="activeIndex == index?'btnAtive':''"
            @click="handleToggle(index)"
        >{{item}}</button>
        <div class="container">
            <div v-for="(item,index) in contents" v-show="activeIndex == index">{{item}}</div>
        </div>
    </div>
</body>
</html>
<script src="./vue.js"></script>
<script>
    var vm = new Vue({
        el:"#app",
        data:{
            btns:["按鈕一","按鈕二","按鈕三"],
            contents:[
                "內容一",
                "內容二",
                "內容三"
            ],
            activeIndex:0
        },
        methods:{
            handleToggle(index){
                this.activeIndex = index;
            }
        }
    })
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM