Search code examples
vue.jsvuejs2css-animationsslidetoggle

Vue slideToggle not smooth


Trying to build an accordion menu. animation of items under the menu is smooth, but menu closing and openings are not smooth.

I have found couple of examples like this https://codepen.io/anon/pen/dgBWjy?editors=0100. but could not fix my problem yet

Here is my code so far. any idea about how can i fix the issue?

<template>
<div class="accord">
    <div v-for="group in groups">
        <a class="menu_grps" v-on:click="group.open = !group.open" v-text="group.name"></a>
        <div id="anim">
        <transition name="slide">
        <div v-show="!group.open">
            <div v-for="item in group.items" v-text="item"></div>
        </div>
        </transition>
        </div>
    </div>
</div>
</template>


<script>
export default {
data() {
    return {
        groups: {
            "group 1": {
                name: "Menu 1",
                open: "false",
                items: ['item1', 'item2']
            },
            "group 2": {
                name: "Menu 2",
                open: "false",
                items: ['item1', 'item2', 'item3']
            },
            "group 3": {
                name: "Menu 3",
                open: "false",
                items: ['item1', 'item2', 'item3', 'item4']
            }
        }
    }
}
}
</script>

<style>

.slide-leave-active,
.slide-enter-active {
transition: all .5s ease;
}
.slide-enter,
.slide-leave-to{
opacity: 0;
transform: translateY(-100%);
margin-bottom: -10px;
}
.menu_grps{
background-color: #BFAEAB;
font-size: 18px;
}
#anim {
overflow: hidden;
}
.accord{
display: flex;
flex-direction: column;
}

</style>

Solution

  • If you change the margin-bottom to -100px as it is below it becomes a lot smoother:

    .slide-enter,
    .slide-leave-to{
    opacity: 0;
    transform: translateY(-100%);
    margin-bottom: -100px;
    }