Collapse div折叠面板功能 vue+jquery完美实现!
代码:(直接复制-即可运行-实现简单,高效)
<!--
* @Descripttion:
* @Author: yizheng.yuan
* @Date: 2020-01-07 17:26:35
* @LastEditors : yizheng.yuan
* @LastEditTime : 2020-01-08 18:55:38
-->
<!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>Collapse</title>
<script src="vue.min.js"></script>
<script src="jquery.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
.deviceBox {
border: 1px solid #eee;
width: 80%;
margin: 10px;
overflow: hidden;
padding: 10px 15px;
border-radius: 5px;
}
.deviceTitle {
background-color: #eee;
padding: 6px 10px;
border-radius: 3px;
cursor: pointer;
margin: 0;
}
.deviceDetail {
background-color: #f5f2f2;
/* display: none; */
padding: 5px 10px;
}
.deviceDetail p {
height: 30px;
line-height: 30px;
}
.leftTitle {
font-weight: bold;
margin-right: 10px;
width: 70px;
display: inline-block;
text-align: right;
}
</style>
</head>
<body>
<div id="app">
<div style="border:1px solid #ccc; padding: 15px; margin: 10px auto; width: 600px;">
<h3>
{{headTitle}}
</h3>
<div class="deviceBox" v-for="(item,index) in devices" :key="item.name">
<h3 class="deviceTitle">
{{item.title}}
</h3>
<transition name="collapseDiv">
<div class="deviceDetail" v-show="item.show">
<p v-show="item.name">
<span class="leftTitle">Name:</span>{{item.name}}
</p>
<p v-show="item.version">
<span class="leftTitle">Version:</span>{{item.version}}
</p>
<p v-show="item.money">
<span class="leftTitle">Money:</span>{{item.money}}
</p>
<p v-show="item.intro">
<span class="leftTitle">Intro:</span>{{item.intro}}
</p>
</div>
</transition>
</div>
</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
headTitle: 'Collapse 折叠面板功能 vue+jquery完美实现!',
devices: [
{
title: '1.vue+jquery实现不同高度,自适应',
name: '1-1.vue+jquery实现不同高度,自适应',
version: '1-2.可以随便增加按钮图标和样式',
show: false
},
{
title: '2.不同高度,自适应',
name: '2-1.vue+jquery实现不同高度,自适应',
version: '2-2.可以随便增加按钮图标和样式',
money: '2-3.可扩展性强',
show: false
},
{
title: '3.自适应',
name: '3-1.vue+jquery实现不同高度,自适应',
version: '3-2.可以随便增加按钮图标和样式',
money: '3-3.可扩展性强,修改简单,容易上手',
intro: '3-4.想知道更多vue知识可以留言评价喔',
show: false
}
]
},
mounted() {
// 获取所有设备详情页的高度属性,赋值给他自身(因为他自身高度是自动的,不知道具体值)
let titleArr = $('.deviceTitle'); //获取所有头部元素
let deviceDiv = $('.deviceDetail'); //获取所有展开元素
$(() => {
// 循环注册点击事件-每点击一个,便展开一个,得益与let和jquery的引用
for (let i = 0; i < titleArr.length; i++) {
$(titleArr[i]).click(function () {
$(deviceDiv[i]).slideToggle();
})
}
})
}
})
</script>
</body>
</html>