效果图:
源码1:
<!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>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
ul{
list-style: none;
}
li{
padding-left: 10px;
line-height: 2;
}
li:hover{
background-color: #eee;
}
.list{
padding: 0 50px;
}
.list .title{
border: 1px solid #ccc;
padding: 10px 5px;
cursor: pointer;
-webkit-user-select: none;
user-select: none;
font-size: 18px;
font-weight: bold;
}
.list .item{
border: 1px solid #ccc;
}
.list:not(:nth-of-type(1)) .item{
display: none;
}
</style>
</head>
<body>
<div id="app">
<div class="list" v-for="(l,i) in lists" :key="i">
<div class="title" @click="toggle($event)">{{l.title}}</div>
<ul class="item">
<li v-for="(item,index) in l.items" :key="index">{{item}}</li>
</ul>
</div>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
lists: [
{ title:'我的同学', items:["张三", "李四", "王五"] },
{ title:'我的同事', items:["张三1", "李四1", "王五1"] },
{ title:'我的家人', items:["张三2", "李四2", "王五2"] }
]
},
methods: {
toggle: function(ev){
var $title = $(ev.target);
$title.parents('#app').find('.item').hide();
$title.next().show(1000);
}
}
})
</script>
</body>
</html>
源码2.
<!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>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
li {
padding-left: 10px;
line-height: 2;
}
li:hover {
background-color: #eee;
}
.list {
padding: 0 50px;
}
.list .title {
border: 1px solid #ccc;
padding: 10px 5px;
cursor: pointer;
-webkit-user-select: none;
user-select: none;
font-size: 18px;
font-weight: bold;
}
.list .item {
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="app">
<div class="list" v-for="(l,i) in lists" :key="i">
<div class="title" @click="toggle(i)">{{l.title}}</div>
<ul class="item" v-show="l.show">
<li v-for="(item,index) in l.items" :key="index">{{item}}</li>
</ul>
</div>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
lists: [
{ title: '我的同学', items: ["张三", "李四", "王五"], show: false },
{ title: '我的同事', items: ["张三1", "李四1", "王五1"], show: true },
{ title: '我的家人', items: ["张三2", "李四2", "王五2"], show: false }
]
},
methods: {
toggle: function (index) {
// ES6的新语法,称为箭头函数, 它是语法糖。
// 当箭头函数函数体中只有一条语句时,return语句可省略。
this.lists.forEach((list, i) => list.show = i == index)
// 回调函数, 回调函数有三个形参,list当前循环中对象,i表示当前循环中的索引
// arr 表示循环的数组本身
// this.lists.forEach(function(list, i, arr){
// // console.log(arr[i]==list);
// // return list.show = i == index;
// if(i == index){
// list.show = true;
// } else {
// list.show = false;
// }
// });
}
}
})
</script>
</body>
</html>
https://www.cnblogs.com/Smiled/p/7610905.html
toggle() 方法切换元素的可见状态。
如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素
$(selector).toggle(speed,callback,switch)
效果图:再点击时候只打开点击哪个的列表,其他的 关闭
安装 npm install --save-dev less-loader less
<template>
<div>
<button v-on:click="decrement">-</button>
<input type="text" size="1" v-model="num">
<button @click="increment">+</button>
<div class="header">
<ul>
<!-- 循环数据在点击调用changeli方法时将当前索引和本条数据传进去,并使用当前数据show的bool值添加或移除样式 -->
<li :class="[{active:item.show}]" @click="changeli(index,item)" v-for="(item,index) in headerData" :key="index">
<!-- 在这里打印出boll值方便查看 -->
{{item.name}}{{item.show}}
<!-- 判断当前这条数据的bool值如果是true就打开二级菜单,如果是false就关闭二级菜单 -->
<ul v-show="item.show">
<!-- 循环二级菜单数据并使用.stop阻止冒泡 -->
<li v-for="(a,index) in item.list" v-on:click.stop="doThis(index)" :key="index">{{a}}</li>
</ul>
</li>
</ul>
</div>
</div>
</template>
<script>
export default {
//props:["num"],//从helloworld组件里面传一个初始值10。父组件到子组件
data(){
return{
num:0,
headerData: [{
name: '客舱服务',
list: ["机上升舱", "呼唤铃", "机上餐食", "我的行程", "安全须知"],
show: true
}, {
name: '精彩华夏',
list: ["品牌介绍", "华夏通程", "航线特惠", "华夏文化"],
show: true
}, {
name: '娱乐',
list: ["影视", "音乐", "游戏", "阅读"],
show: true
}]
}
},
methods:{
increment(){
this.num++
},
decrement(){
if (this.num<=0){
alert('受不了啦,宝贝不能再减少啦')
this.num=0;
}else {
this.num--;
}
},
changeli: function(ind, item) {
// 先循环数据中的show将其全部置为false,此时模板里的v-if判断生效关闭全部二级菜单,并移除样式
this.headerData.forEach(i => {
// 判断如果数据中的headerData[i]的show属性不等于当前数据的show属性那么headerData[i]等于false
if (i.show !== this.headerData[ind].show) {
i.show = false;
};
});
// 取反(true或false)
item.show = !item.show;
console.log(item.name)
},
doThis: function(index) {
alert(index)
}
}
}
</script>
<style lang="less" scoped>
input{
text-align: center;
}
.header {
width: 20%;
background-color: rgb(0, 50, 116);
color: #ffffff;
>ul {
width: 100%;
// @include clearfix;
>li {
width: 100%;
// border: 1px solid #ffffff;
cursor: pointer; // float: left;
color: 20px;
text-align: center;
line-height: 60px;
&:hover {
background-color: rgb(7, 27, 53);
}
>ul {
width: 100%;
background: rgb(0, 50, 116);
li{
&:hover{
background: rgb(0, 50, 116);
}
}
}
}
.active {
background-color: rgb(12, 48, 94);
}
}
}
</style>