How to load the image successfully, fail to start, get the width and height of the image

code:

 // new一个图片对象
var img = new Image();
// 获取 图片
img.src = '5bd2c6c01ffcd01970b8940c3b60949.jpg'

/**
   * onload:图片加载成功后触发
   * onerror: 图片加载失败后触发
*/
img.onload = function() {
    /**
        * 获取当前图片的宽高
    */
    console.log(img.width, img.height)
}

Effect:

 

Console output:

 

Guess you like

Origin blog.csdn.net/qq_52421092/article/details/130049104