Vue CSS模擬菜單點擊變色


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

<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">
    <script src="./node_modules/vue/dist/vue.js" type="text/javascript" charset="utf-8"></script>
    <script src="./node_modules/element-ui/lib/index.js"></script>
    <link rel="stylesheet" href="./node_modules/element-ui/lib/theme-chalk/index.css">
    <title></title>
    <style>
        .active {
            color: red;
            background-color: lightslategrey;
        }

        ul,
        li {
            padding: 0;
            margin: 0;
            list-style: none;
            line-height: 40px;
        }
    </style>
</head>

<body>
    <div id="app">
        <ul>
            <li v-for="(item,index) in movies" :class="{active:selectIndex==index}" @click="liClick(index)">
                {{item}}
            </li>
        </ul>
    </div>
    <script>
        const vm = new Vue({
            el: "#app",
            data() {
                return {
                    movies: ["決戰中途島", "誤殺", "葉問4", "唐人街探案3", "終結者:黑暗命運"],
                    selectIndex: 0
                }
            },
            methods: {
                liClick(index) {
                    this.selectIndex = index
                }
            },
        })
    </script>
</body>

</html>


免責聲明!

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



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