Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML offsetLeft delays to change

I have four canvas in my screen, positioned two above and two below.

Each one have a button that makes possible to maximize the canvas, hiding the others.

This button is positioned above each canvas, with absolute position based on offsetTop and offsetLeft of the canvas.

However, when I maximize or minimize a canvas, the button formula updates only the width property.

The strange thing is that if I resize the screen, which also calls resize function, everything goes to the right place.

Initial screen

After maximizing

Then after resizing screen

EDIT: Additional information: I am using VueJS and, in order to hide the other canvas, I apply v-show="false" to them parent divs, which only applies display: none.

Some snippets:

Initial resize and listener:

window.onload = function () {
    resizeAll();
    window.addEventListener('resize', resizeAll, false);
};

The resize hub:

function resizeAll () {
    vue.$refs.panelOne.resizeDefault();
    // ...
    vue.$refs.panelN.resizeDefault();
}

The panel's resize default and resize method. The "expandStyles" is the css styles applied to the button:

resizeDefault() {
        let dimensions;
        if (this.expanded) {
            dimensions = getScreenDimensions();
        } else {
            dimensions = getHalfScreenDimensions();
        }
        this.resize(dimensions.width, dimensions.height);
    }

resize (width, height) {
        this.canvas.width = width;
        this.canvas.height = height;
        this.expandStyles.top = (this.canvas.offsetTop + 10) + 'px';
        this.expandStyles.left = (this.canvas.offsetLeft + this.canvas.width - 40) + 'px';
        drawInterface.redraw();
    }

And finally, the dimension getters:

function getScreenDimensions () {
    return {
        width: window.innerWidth - 310,
        height: window.innerHeight * 0.92
    };
}

function getHalfScreenDimensions () {
    return {
        width: (window.innerWidth - 310) / 2,
        height: (window.innerHeight * 0.92) / 2
    };
}
like image 882
Luiz Avatar asked Oct 25 '17 11:10

Luiz


People also ask

What is element offsetLeft?

Definition and Usage The offsetLeft property returns the left position (in pixels) relative to the parent. The returned value includes: the left position, and margin of the element. the left padding, scrollbar and border of the parent.

What is CSS offsetLeft?

The HTMLElement. offsetLeft read-only property returns the number of pixels that the upper left corner of the current element is offset to the left within the HTMLElement.

What is offsetTop Javascript?

The offsetTop property returns the top position (in pixels) relative to the parent. The returned value includes: the top position, and margin of the element. the top padding, scrollbar and border of the parent.


1 Answers

I agree with S.P.H.I.N.X , it seems like the problem lies with propagation of reactive properties in this.expandStyles to actual DOM. Would be useful if you can share the part of the template which has your canvases and positioning application.

Meanwhile, if setTimeOut doesn't feel right to you, you may do things more "Vue-way" by using Vue: nextTick to set order of operations (styles recalculation, redraw).

like image 66
Gleb Varenov Avatar answered Oct 12 '22 23:10

Gleb Varenov