| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 
 | 该练习综合了数组渲染、对象渲染,v-if、v-show、key 事件方法等知识。
 <!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>syl-vue-test</title>
 <style>
 * {
 padding: 0;
 margin: 0;
 }
 a {
 text-decoration: none;
 color: #fff;
 }
 ul {
 list-style: none;
 }
 nav,
 ul {
 width: 100%;
 display: flex;
 flex-direction: row;
 justify-content: center;
 background: yellowgreen;
 }
 nav > ul > li {
 width: 20%;
 height: 100%;
 text-align: center;
 line-height: 50px;
 }
 nav > ul > li:hover {
 box-shadow: 1px 0px 10px #fff;
 }
 nav > ul > li > ul {
 display: flex;
 flex-direction: column;
 }
 nav > ul > li > ul > li {
 box-shadow: 1px 0px 10px #fff;
 }
 nav > ul > li > a {
 text-transform: uppercase;
 }
 </style>
 
 <script src="vue.min.js"></script>
 </head>
 <body>
 <div id="app">
 <nav>
 <ul>
 
 
 <li
 v-for="(nav,index) in navbar"
 :key="index"
 @mouseover="currentIndex(index)"
 @mouseout="changeIndex"
 >
 
 <a href="javascript:;">{{nav.name}}</a>
 
 
 <ul v-if="nav.child" v-show="current===index">
 <li v-for="item in nav.child">
 <a href="javascript:;">{{item}}</a>
 </li>
 </ul>
 </li>
 </ul>
 </nav>
 </div>
 <script>
 var app = new Vue({
 el: "#app",
 data: {
 
 navbar: [
 {
 name: "home",
 child: ["homeItem", "homeItem"],
 },
 {
 name: "contact",
 child: ["contactItem", "contactItem"],
 },
 {
 name: "about",
 },
 ],
 
 current: null,
 },
 methods: {
 
 currentIndex: function (index) {
 this.current = index;
 },
 
 changeIndex: function () {
 this.current = null;
 },
 },
 });
 </script>
 </body>
 </html>
 
 
 |