admin管理员组文章数量:1389955
I have a number of items on my page that can be collapsed. Here's a codepen to illustrate.
Vueponent('block', {
template: '#block',
props: [ 'title', 'items', 'collapsed' ],
});
new Vue({
el: '#app',
data: {
first: [
{ name: 'Item 1' },
{ name: 'Item 2' },
{ name: 'Item 3' }
],
second: [
{ name: 'Item 1' },
{ name: 'Item 2' },
{ name: 'Item 3' }
]
}
});
#app {
display: flex;
flex-direction: column;
}
.block {
$padding: 10px;
margin: 20px;
.title {
color: white;
background: grey;
padding: $padding;
font-weight: bold;
font-size: 22px;
cursor: pointer;
}
.list {
position: relative;
z-index: -1;
padding: $padding;
font-size: 16px;
border: 1px solid lightgrey;
}
.list-item:not(:last-child) {
padding-bottom: $padding;
}
}
.slide-out-enter-active,
.slide-out-leave-active {
transition: all 0.3s;
}
.slide-out-enter,
.slide-out-leave-to {
opacity: 0;
transform: translateY(-40px);
}
<script src=".3.4/vue.js"></script>
<div id="app">
<block :items="first" title="First block"></block>
<block :items="second" title="Second block"></block>
</div>
<script type="text/x-template" id="block">
<div class="block">
<div class="title" @click="collapsed = !collapsed">{{ title }} (click to toggle)</div>
<transition name="slide-out">
<div v-if="!collapsed" class="list">
<div class="list-item" v-for="item in items">
{{item.name}}
</div>
</div>
</transition>
</div>
</script>
I have a number of items on my page that can be collapsed. Here's a codepen to illustrate.
Vue.ponent('block', {
template: '#block',
props: [ 'title', 'items', 'collapsed' ],
});
new Vue({
el: '#app',
data: {
first: [
{ name: 'Item 1' },
{ name: 'Item 2' },
{ name: 'Item 3' }
],
second: [
{ name: 'Item 1' },
{ name: 'Item 2' },
{ name: 'Item 3' }
]
}
});
#app {
display: flex;
flex-direction: column;
}
.block {
$padding: 10px;
margin: 20px;
.title {
color: white;
background: grey;
padding: $padding;
font-weight: bold;
font-size: 22px;
cursor: pointer;
}
.list {
position: relative;
z-index: -1;
padding: $padding;
font-size: 16px;
border: 1px solid lightgrey;
}
.list-item:not(:last-child) {
padding-bottom: $padding;
}
}
.slide-out-enter-active,
.slide-out-leave-active {
transition: all 0.3s;
}
.slide-out-enter,
.slide-out-leave-to {
opacity: 0;
transform: translateY(-40px);
}
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.3.4/vue.js"></script>
<div id="app">
<block :items="first" title="First block"></block>
<block :items="second" title="Second block"></block>
</div>
<script type="text/x-template" id="block">
<div class="block">
<div class="title" @click="collapsed = !collapsed">{{ title }} (click to toggle)</div>
<transition name="slide-out">
<div v-if="!collapsed" class="list">
<div class="list-item" v-for="item in items">
{{item.name}}
</div>
</div>
</transition>
</div>
</script>
I managed to animate the collapse. My problem however, is that if you expand the first block, the second block immediately jumps to its final position. I would like the second block to slide down while the first block is expanding.
Any thoughts?
Share Improve this question edited Oct 31, 2018 at 7:07 fikkatra asked Oct 30, 2018 at 16:22 fikkatrafikkatra 5,8424 gold badges44 silver badges73 bronze badges2 Answers
Reset to default 3The issues is that your layout does not reflow due to the nature of the transform
property after the .list-item
s are added to your .blocks
s. You can read about 3 diff techniques that could be used for what you are doing here.
My suggestion to use the min-height
property instead and get rid of the Vue transition and simply toggle the class on your .list
node. Below are the adjustments that you'd need:
CSS:
// strip all of the .slide-out-* classes
.list {
....
max-height: 100px;
transition: all 1s;
}
.block .list.is-collapsed {
transition: all 1s;
max-height: 0;
padding-top: 0;
padding-bottom: 0;
}
HTML:
remove tags &
<div class="list" :class="{'is-collapsed': collapsed}"> <--- removed 'v-if'
You could also animate the margin property at the same time as translating. See this codepen example
basically just change your enter
and leave-to
css to something like this
.slide-out-enter,
.slide-out-leave-to {
opacity: 0;
transform: translateY(-100px);
margin-bottom: -100px;
}
本文标签: javascriptVuejs transitions animate height while using slide out animationStack Overflow
版权声明:本文标题:javascript - Vue.js transitions: animate height while using slide out animation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744574994a2613567.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论