I got a list of grid items , and a button switching between “see more” and “see less”. On click on the “see more” button, all the list is displayed. on click on the see less, only 3 items are displayed. we got a nice transition when displaying all the items, but when we hide them, all the elements on the bottom don’t follow the motion. How can I make the transition smooth and don’t leave the user at the middle of the paragraphe ?
template exemple :
JavaScript
x
18
18
1
<div>
2
<transition-group name="list" class="grid">
3
<div v-for="(content,index) in contents" :key="index" class="card">
4
<h4>
5
{{index}}
6
</h4>
7
</div>
8
</transition-group>
9
10
<button @click="onClickSeeButton">
11
{{ seeButton }}
12
</button>
13
14
<p>
15
Some text
16
</p>
17
</div>
18
I used those css properties for the transition
JavaScript
1
7
1
.list-enter-active, .list-leave-active {
2
transition: all 1s;
3
}
4
.list-enter, .list-leave-to {
5
opacity: 0;
6
}
7
the Script goes as follow :
JavaScript
1
21
21
1
new Vue({
2
el: "#app",
3
data: {
4
els: [1,2,3,4,5,6,7,8,9,10,11,12], // from 0 to infinite
5
seeMore: true
6
},
7
computed: {
8
contents: function(){
9
return this.seeMore ? this.els.filter((el,index) => index < 3 ) : this.els
10
},
11
readButton: function(){
12
return this.seeMore ? 'see more' : 'see less'
13
}
14
},
15
methods: {
16
onClickReadButton: function(){
17
this.seeMore = !this.seeMore
18
}
19
}
20
})
21
https://jsfiddle.net/ghivalla/v5me216c/3/
Advertisement
Answer
Try this
JavaScript
1
8
1
.list-enter-active, .list-leave-active {
2
transition: all 1s;
3
opacity: 1;
4
}
5
.list-enter, .list-leave-to {
6
opacity: 0; height: 0;
7
}
8
https://jsfiddle.net/w4v9g6us/
Also here is a good place to read more about vue and transitions https://v2.vuejs.org/v2/guide/transitions.html