Javascript教程
JavaScript基础
JavaScript Objects
JavaScript BOM
JavaScript DOM
JavaScript OOP
JavaScript Cookies
JavaScript事件
JavaScript异常
JavaScript常用

JavaScript 获取图像尺寸的程序

获取图像尺寸的 JavaScript 程序

在本例中,您将学习编写一个 JavaScript 程序来获取图像的尺寸。

示例: 获取图像的尺寸

// program to get the dimensions of an image
const img = new Image();
// get the image
img.src = '//cdn.programiz.com/sites/tutorial2program/files/cover-artwork.png';
// get height and width
img.onload = function() {
  console.log('width ' + this.width)
  console.log('height '+ this.height);
}
输出
width 1040
height 922
在上面的程序中, new Image()构造函数用于创建一个图像对象。
Image() 构造函数创建一个新的图像元素实例。
img.src 然后用于使用图像 URL 源添加图像。
img.onload() 函数用于访问图像的高度和宽度。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4