R
R
Renhor2019-04-21 15:29:47
Vue.js
Renhor, 2019-04-21 15:29:47

How to get the height of a child component?

Good day! I'm making a slider with Vue. Decided to add Fade animation. The structure is something like this:

<slider>

    <slide> 1 </slide>
    <slide> 2 </slide>
    <slide> 3 </slide>

</slider>

In the parent component (slider) I get the slides through $slots.
There is no problem with the normal translate animation, because elements are not absolute.
For the fade animation, you need to position the slides absolutely and switch their opacity.
But also, the parent component (its DOM element) needs to set the height of the highest slide. I'm trying to do it through the mounted hook - however, the height is calculated inadequately.
this.slides.forEach((item) => {
    console.log(item.elm.clientHeight)
});

Gives out 1559, 1915, 2371 although the height of the slides is 431, 315 and 347.
Tell me what's the matter?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Renhor, 2019-04-21
@Renhor

Solution:
Use the this.$nextTick() method
Inside the mounted hook. Without it, there is no guarantee that child elements will be mounted, so a bug with height appears.

L
lavezzi1, 2019-04-21
@lavezzi1

I would do this https://jsfiddle.net/9vb8x3mt/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question