Issue encountered when attempting to retrieve dimensions of a div element

Using Vue and facing an issue with a div setup that I have:

<div :style="'width:' + imageSize.width + 'px; height:' + imageSize.height + 'px'" id="image-container"></div>

The dimensions are stored in the data like this:

data() {
return {
  ...
  imageSize: {
    width: 500,
    height: 500
  }
  ...
}

In addition, there are two buttons to zoom in and out the div:

<v-btn rounded color="white" class="ma-2" @click="zoom('+')">
   <v-icon>mdi-magnify-plus-outline</v-icon>
</v-btn>
<v-btn rounded color="white" class="ma-2" @click="zoom('-')">
  <v-icon>mdi-magnify-minus-outline</v-icon>
</v-btn>

The zoom function is as follows:

zoom(type) {
  if(type == '+') {
     this.imageSize.height += 100;
     this.imageSize.width += 100;
     console.log('Zoom in clicked!')
  } else {
     this.imageSize.height -= 100;
     this.imageSize.width -= 100;
     console.log('Zoom out clicked!')
  }
  const { offsetWidth, offsetHeight } = document.getElementById("image-container");
  console.log(offsetWidth);
  console.log(offsetHeight);
},

Clicking the button to zoom in increases the size of the div by 100px per side. However, when trying to retrieve the size of the div to save in the data, the size remains unchanged. It also seems to revert back to the previous zoom size upon clicking again. Refer to the console screenshot below: Screenshot of the console

Expectation is to get 600 instead of 500 after zooming in by 100px. Appreciate any help on this matter!

Answer №1

setTimeout(() => {
    const { sizeWidth, sizeHeight } = document.getElementById("pic-container");
  console.log(sizeWidth);
  console.log(sizeHeight);
}, 200)

Give this a shot following the if else condition.

This scenario occurred to me a couple of days back while working on animations with gsap. I believe that when the image's height or width is updated, other statements execute before it, as there is a delay in updating it.

I tested out this solution and it resolved my issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Cordova: displaying websites in a webview

When loading an external website with different HTML files in an iframe within a Cordova webview on iOS, I encountered an issue. Whenever I click on a link within the iframe that points to another HTML page, it opens in the Safari browser instead of changi ...

Transferring scoped slots across several components

I have two different components in my Vue project. One is a list component called "List.vue", and the other one, named "SearchAndList", wraps the list with additional features. From within the "Page.vue" file, I am trying to call the SearchAndList componen ...

Changing text color with JQuery animation

Is there a way to animate text color using jQuery/jQuery UI? I want the text color to change from #000 to #F00 when an event is triggered, then fade back to #000 over 3 seconds. I attempted using effect("highlight", {}, 3000) with the highlight effect, bu ...

No matter what, the Django authenticate function will consistently return None

I have been facing challenges in implementing registration/login/logout functionality. While user registration is successful and the user data appears in Django admin as well as in the database, I am encountering issues with the login functionality. Upon c ...

Workers in Async.js queue failing to complete tasks

Creating a job queue to execute copy operations using robocopy is achieved with the following code snippet: interface copyProcessReturn { jobId: number, code: number, description: string, params: string[], source: string, target: s ...

The error message states that there is a missing parent component for <MenuItems /> with the <Menu /> component in the slot

Currently, I am in the process of developing a custom Vue3 component known as <Dropdown>. This particular Dropdown component relies on several other components including: Menu, MenuItems, MenuItem, and MenuButton from the headlessui/vue library. Th ...

What steps do I need to take in order to show the present value using a range input?

Hey there! I'm working on a code in HTML that includes an input of type range. Here's what it looks like: This is my input: <input type="range" min="1" max="100" value="50" class="slider" id="myRange"> Unfortunately, I'm unable to s ...

Analyzing the object for interface compatibility

When I receive a query string in one of my REST endpoints using koa-router, each value of the query string object parameter is always a string: { count: "120", result: "true", text: "ok" } Within my codebase, I have an Interface that represents the ...

Loading components dynamically with axios is a valuable feature

Can this be achieved? There is a spinner component. axios: action() { SPINNER (component) -- activate axios.get('/store', { params: { something } }) .then ((resp) => { SPINNER (component) -- ...

Navigating JSON data along diverging paths

Greetings everyone, I am interested in creating a unique JSON script that has the capability for objects to either retrieve a value or navigate through a data path. For instance: { "A" : { "B" : "123" } } In the given example, when I access the ...

I attempted to utilize draggable.helper, but my intention was to include the helper in the DOM, which unfortunately caused an issue

The data sample I have been working on is located at this link: http://jsfiddle.net/aLnJG/3/ Basically, I am using draggable to create a helper which is an input box, and then when dropped, I want to append the helper to the canvas. Here is the code sni ...

Creating dynamic HTML elements by utilizing both jQuery and native JavaScript within the DOM

I have an old application that I'm revamping, and instead of using the node's id, I want to apply the DOM structure to its class. Here is a snippet of my code where I am attempting to combine jQuery (selecting the node by its class) with the exi ...

What is the correct way to set up favicon.ico in a Vue.js webpack project?

After setting up my Vue webpack project with vue-cli, I encountered an issue. First, I created the project using the command vue init webpack myproject. Then, to run the project in development mode, I used: npm run dev However, upon running the project, ...

How can nested json be sorted effectively based on two specific fields?

Example Data: [{ 'ID': objID(abc123), 'Department': 'IT', 'Employees': [ { 'ID': 3, 'StartDate': '24-12-2022T08:30', 'active': true }, { ...

What steps should I take to create a functional image scrolling effect similar to the one shown in this example?

Recently, I came across and was intrigued by the "image slide effect" featured on their homepage. The way the background image changes as you scroll up, leading you through different introductory slides before reaching the main content of the site caught ...

I am looking to switch themes on the homepage using a button from an imported component

I have successfully created a toggle button on my main page in app.js to switch between dark and light themes. However, I am facing difficulty in putting the button inside my nav component and using it from that page imported in app.js. Can anyone guide me ...

The body's width is more compact compared to one of my containers in HTML/CSS

After completing my code for a beginner's HTML/CSS project, I realized that the main parent container holding two smaller containers needed to be set at specific widths. The two inner containers had to be exactly 650px and 270px wide, while the main p ...

Vue's production mode experiencing breaks

My code was working perfectly fine with lodash imported as (import _ from 'lodash'), even though I had not installed the lodash package. However, today it started throwing an error in the chunkvendor.js file: ...

css scroll-snap: highlighting the snapped element

I created a horizontal grid containing multiple cards that scroll smoothly using CSS scroll-snap functionality when navigated with a mouse or touchscreen. The issue arises when attempting to navigate the grid using a keyboard. After moving through the gri ...

"The server is refusing to process the request because it does not allow POST methods, only GET methods are

After successfully creating a web api in ASP.NET Web API, I encountered an issue when accessing the site hosted on Vercel. While the GET methods worked fine, the POST methods did not. This was due to the backend API being on my local system, causing a conf ...