nextTick cannot get the width and height of the picture

nextTickThe method itself cannot directly obtain the width and height of the image, it is only used to execute the callback function after the next DOM update cycle ends. To get the width and height of the picture, you need to use other methods to get it in the callback function.

A common method is to use Imagean object to load an image, and get the width and height of the image after loading. You can set onloadan event listener to execute the callback function after the image is loaded.

The following is a sample code that demonstrates how to use nextTickand Imageobject to get the width and height of an image:

import {
    
     nextTick } from 'vue';

export default {
    
    
  mounted() {
    
    
    nextTick(() => {
    
    
      const image = new Image();
      image.src = 'path/to/image.jpg';
      image.onload = () => {
    
    
        const width = image.width;
        const height = image.height;
        console.log(`Image size: ${
      
      width}px x ${
      
      height}px`);
      };
    });
  }
}

In the above example, an object is first created Imageand srcthe property is set as the path of the picture. Then, by setting onloadan event listener, the callback function is executed after the image is loaded. In the callback function, you can get the width and height of the picture and perform corresponding operations.

Note that `path/to/image.jpg

Tool collection: https://aiburgeon.com/siteCollection/

insert image description here

おすすめ

転載: blog.csdn.net/qq_25741071/article/details/132608810