Aks*_*iti 0 css vue.js bootstrap-4 vuejs2
我正在制作一个带有折叠显示/隐藏的侧栏菜单。使用我当前的 css,折叠并不平滑,并且看起来很强力或者看起来有些奇怪。
我想要一张在关闭项目时能够平滑过渡的幻灯片。但就我而言,发生的情况是当某个项目已经打开并且单击下一个项目(打开)时。看起来切换正在被强制执行,并且列表的折叠似乎并不顺利。
对此有什么更好的方法,请提出一些更好的方法。
我不知道我的方法是否正确,或者我在这里遗漏了什么?
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 !== ''
}
}
})Run Code Online (Sandbox Code Playgroud)
.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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
您可以使用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>
Run Code Online (Sandbox Code Playgroud)
主要不是隐藏,而是<ul>将v-for数组更改为空或从空更改。请注意,我还更改了il的键,因为您使用了无效的道具。
并为过渡添加以下 CSS:
.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; }
}
Run Code Online (Sandbox Code Playgroud)
在这里更新了JSFiddle。下面演示。
<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>
Run Code Online (Sandbox Code Playgroud)
.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; }
}
Run Code Online (Sandbox Code Playgroud)
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 !== ''
}
}
})Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6107 次 |
| 最近记录: |