问题描述
我正在制作带有折叠显示/隐藏的侧边栏菜单.以我目前的CSS来说,崩溃并不顺利,似乎力量十足或看起来有些怪异.
I am making a side bar menu with collapse show/hide. With my current css the collapse is not smooth and it seems force full or looks some weird.
我想要一个在关闭项目时具有平滑过渡的幻灯片.但是在我的情况下,发生的事情是某个项目已经打开,而下一个项目被单击(打开).那里好像是在强行进行切换,而且列表似乎没有平滑折叠.
I wanted a slide with smooth transition when the item is being closed. But here in my case what's happening is when some item is already open and next item is clicked(for open). There it looks like toggle is being forcefully and it does not seems smooth collapsing of the list.
有什么更好的方法,请提出一些更好的方法.
What could be the better approach for this please suggest some better ways.
我不知道我的方法是否正确,或者我在这里遗漏了什么?
I don't know whether my approach is correct or I am missing something here?
new Vue({
el: '#app',
methods: {
setActiveItemId(itemIndex) {
if (itemIndex === this.activeItemId) {
this.activeItemId = ''
return
}
this.activeItemId = itemIndex
}
},
data() {
return {
message: 'Hello Vue.js!',
activeItemId: '',
sideBar: [{
name: "Dashboard",
url: "/dashboard",
icon: "ti-world",
children: [{
name: "Buttons",
url: "/components/buttons",
icon: "fa-book",
},
{
name: "Social Buttons",
url: "/components/social-buttons",
icon: "icon-puzzle",
}
]
},
{
name: "Components",
url: "/components",
icon: "ti-pencil-alt",
children: [{
name: "Buttons",
url: "/components/buttons",
icon: "fa-book",
},
{
name: "Social Buttons",
url: "/components/social-buttons",
icon: "icon-puzzle",
}
]
},
{
name: "Validation",
url: "/components",
icon: "ti-pencil-alt",
children: [{
name: "Buttons",
url: "/components/buttons",
icon: "fa-book",
},
{
name: "Social Buttons",
url: "/components/social-buttons",
icon: "icon-puzzle",
}
]
}
]
}
},
computed: {
isActive() {
return this.activeItemId !== ''
}
}
})
.collapse.show {
display: block;
}
.collapse {
display: none;
}
.list-unstyled {
padding-left: 0;
list-style: none;
}
.collapse.list-unstyled {
padding-left: 15px;
}
nav.side-navbar {
background: #fff;
min-width: 250px;
max-width: 250px;
color: #000;
-webkit-box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);
box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);
z-index: 999;
}
nav.side-navbar ul a:hover {
background: orange;
color: #fff !important;
}
nav.side-navbar ul a {
padding: 10px 15px;
text-decoration: none;
display: block;
font-weight: 300;
border-left: 4px solid transparent;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://unpkg.com/vue"></script>
<div id="app">
<nav class="side-navbar">
<ul class="list-unstyled">
<li>
<a>
<i class="ti-home"></i>Home</a>
</li>
<li v-for="(x, itemIndex) in sideBar" :key="itemIndex">
<a @click="setActiveItemId(itemIndex)">
<i class="fa" :class="x.icon"></i>{{x.name}}
</a>
<ul :id="x.id" class="collapse list-unstyled" :class="{'show':activeItemId === itemIndex && isActive}">
<li v-for="y in x.children" :key="y.id">
<a>{{y.name}}</a>
</li>
</ul>
</li>
</ul>
</nav>
</div>
推荐答案
您可以使用 Vue的列表转换 (<transition-group>
标签).
将子列表ul
更改为:
<ul :id="x.id" class="collapse list-unstyled show">
<transition-group name="list">
<li v-for="y in (activeItemId === itemIndex && isActive ? x.children : [])" :key="y.name">
<a>{{y.name}}</a>
</li>
</transition-group>
</ul>
主要是将v-for
数组更改为空,而不是隐藏<ul>
.请注意,由于您使用的是无效道具,我还更改了il
的键.
Mainly instead of hiding the <ul>
we are changing the v-for
array to/from empty. Notice I also changed the il
's keys, as you were using an invalid prop.
并为过渡添加以下CSS:
And add the following CSS for the transitions:
.list-enter {
opacity: 0;
}
.list-enter-active {
animation: slide-in .5s ease-out forwards;
}
.list-leave-to, .list-leave-active {
opacity: 0;
animation: slide-out .5s ease-out forwards;
}
@keyframes slide-in {
from { height: 0; } to { height: 40px; }
}
@keyframes slide-out {
from { height: 40px; } to { height: 0; }
}
更新了 此处是JSFiddle .下面的演示.
Updated JSFiddle here. Demo below.
new Vue({
el: '#app',
methods: {
setActiveItemId(itemIndex) {
if (itemIndex === this.activeItemId) {
this.activeItemId = ''
return
}
this.activeItemId = itemIndex
}
},
data() {
return {
message: 'Hello Vue.js!',
activeItemId: '',
sideBar: [{
name: "Dashboard",
url: "/dashboard",
icon: "ti-world",
children: [{
name: "Buttons",
url: "/components/buttons",
icon: "fa-book",
},
{
name: "Social Buttons",
url: "/components/social-buttons",
icon: "icon-puzzle",
}
]
},
{
name: "Components",
url: "/components",
icon: "ti-pencil-alt",
children: [{
name: "Buttons",
url: "/components/buttons",
icon: "fa-book",
},
{
name: "Social Buttons",
url: "/components/social-buttons",
icon: "icon-puzzle",
}
]
},
{
name: "Validation",
url: "/components",
icon: "ti-pencil-alt",
children: [{
name: "Buttons",
url: "/components/buttons",
icon: "fa-book",
},
{
name: "Social Buttons",
url: "/components/social-buttons",
icon: "icon-puzzle",
}
]
}
]
}
},
computed: {
isActive() {
return this.activeItemId !== ''
}
}
})
.collapse.show {
display: block;
}
.collapse {
display: none;
}
.list-unstyled {
padding-left: 0;
list-style: none;
}
.collapse.list-unstyled {
padding-left: 15px;
}
nav.side-navbar {
background: #fff;
min-width: 250px;
max-width: 250px;
color: #000;
-webkit-box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);
box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);
z-index: 999;
}
nav.side-navbar ul a:hover {
background: orange;
color: #fff !important;
}
nav.side-navbar ul a {
padding: 10px 15px;
text-decoration: none;
display: block;
font-weight: 300;
border-left: 4px solid transparent;
}
.list-enter {
opacity: 0;
}
.list-enter-active {
animation: slide-in .5s ease-out forwards;
}
.list-leave-to, .list-leave-active {
opacity: 0;
animation: slide-out .5s ease-out forwards;
}
@keyframes slide-in {
from { height: 0; } to { height: 40px; }
}
@keyframes slide-out {
from { height: 40px; } to { height: 0; }
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://unpkg.com/vue"></script>
<div id="app">
<nav class="side-navbar">
<ul class="list-unstyled">
<li>
<a>
<i class="ti-home"></i>Home</a>
</li>
<li v-for="(x, itemIndex) in sideBar" :key="itemIndex">
<a @click="setActiveItemId(itemIndex)">
<i class="fa" :class="x.icon"></i>{{x.name}}
</a>
<ul :id="x.id" class="collapse list-unstyled show">
<transition-group name="list">
<li v-for="y in (activeItemId === itemIndex && isActive ? x.children : [])" :key="y.name">
<a>{{y.name}}</a>
</li>
</transition-group>
</ul>
</li>
</ul>
</nav>
</div>
这篇关于Vue js引导程序在折叠时添加动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!