1

I'm trying to get the height of an element. If I try to get it too early, the height is 0px. I tried many different ways to get the height: in mounted(), in created(), on nextTick(), in updated(), and using window.eventListener, but I either get 0px or a size much smaller than I'd expect it to be (~117px). When I run the same command in the console on the page, I get the size I would expect it to be (~278px). If I resize the window, everything works fine. How can I get it to get the right height from the start?

In a vue file, I have:

    mounted() {
        this.$nextTick(() => {
            this.vtextHeight = (document.getElementById(this.item.id).offsetHeight - document.getElementsByClassName("v-card__title")[0].offsetHeight) + 'px';
            console.log("next tick", this.vtextHeight);
        });
    },

    updated() {
        this.vtextHeight = (document.getElementById(this.item.id).offsetHeight - document.getElementsByClassName("v-card__title")[0].offsetHeight) + 'px';
        console.log("updated", this.vtextHeight);
    },

    created () {
        var page = this;
        var itemId = this.item.id;

        window.addEventListener('load', () => {
            page.vtextHeight = (document.getElementById(itemId).offsetHeight - document.getElementsByClassName("v-card__title")[0].offsetHeight) + 'px';
            console.log("on load");
        });
        window.addEventListener('resize', () => {
            page.vtextHeight = (document.getElementById(itemId).offsetHeight - document.getElementsByClassName("v-card__title")[0].offsetHeight) + 'px';
            console.log("resize", page.vtextHeight);
        });
    }

On the page load:

  • "on load" never gets logged
  • "next tick 117px" and "updated 117px" gets logged, but that's smaller than I'd expect it to be

On a 1px window resize:

  • "resize 278px" and "updated 278px" gets logged, which is the value I'd expect it to be.
Emma
  • 51
  • 1
  • 7
  • What does your template look like? What sort of element are you trying to query? – Phil Mar 10 '20 at 06:20
  • I'm using Vuetify's v-card, v-card-title, and v-card text. The v-card is inside of a div created by a dependency I'm using, but it has a set width and height and has the id stored in this.item.id. I want to make the v-cart-text scrollable, but I seem to only be able to do this by setting the height of the v-card text as the height of the div minus the v-card-title – Emma Mar 10 '20 at 06:30

1 Answers1

1

you can use vue specific ref approach to access DOM elements.

For example :

 <div class="v-card__title" ref="vCard" :style="{'width': '100px', 'height': '200px'}"> 

Access the element height like this,

this.$refs.vCard.offsetHeight + 'px';

You can learn ref attribure here

View similar stack overflow question Get element height with Vuejs here.

tony19
  • 125,647
  • 18
  • 229
  • 307
Jeni
  • 320
  • 2
  • 12
  • It still shows 117px when the page is first rendered. I have no idea where it's getting that size from. – Emma Mar 10 '20 at 07:23
  • I was actually looking at the wrong console log, but I'm having issues with using $refs. I logged: `var cardRef = 'card-' + itemId; console.log("getting card height of: " + cardRef); console.log("refs again", this.$refs); console.log("ref card: ", this.$refs[cardRef]);` Printed, I get: `getting card height of: card-XEQxYe7e refs again {} card-XEQxYe7e: [VueComponent] card-0MG4FiRWN: [VueComponent] card-42r-tHs6L: [VueComponent] card-e5_Ip9gj: [VueComponent] __proto__: Object ref card: undefined` When I hover over the name, it shows ""card-XEQxYe7e"". Why 4 quotes? – Emma Mar 10 '20 at 18:44
  • Also, my code in the v-card is: `:ref="'card-' + item.id"` – Emma Mar 10 '20 at 18:50
  • Following what was done here: https://forum.vuejs.org/t/solved-this-refs-key-returns-undefined-when-it-really-is/1226/2 I found that refs isn't populated yet, but I'm calling the code in mounted() so I don't understand what I'm doing wrong. – Emma Mar 10 '20 at 19:05
  • So removing the '-' seemed to work, and now I can get the ref properly, but `this.$refs[cardRef][0].clientHeight` is undefined – Emma Mar 10 '20 at 19:29
  • Solved! Instead of using clientHeight, I dug deep into the ref and found a hidden variable that had the height in it, which I then used. Thanks! – Emma Mar 11 '20 at 04:12