jQuery:如何获取外部图像的尺寸
jQuery: how to get the dimensions of a external image?
寻找一种获取外部图像尺寸(宽度和高度)的方法。
我使用了prop()
和attr()
,但它们不返回任何值。只是一个错误。
例:
<a href="pathtotheimage">some link</a>
jQuery
var path = 'https://source.unsplash.com/random/600x300/?montreal';
$("<img/>").attr('src', path).load(function() {
console.log(this.width, this.height);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
香草Javascript
var path = 'https://source.unsplash.com/random/600x300/?montreal';
var img = new Image();
img.src = path;
img.addEventListener('load', function() {
console.log(this.width, this.height);
});
看起来没有人提供有效的香草 js 答案。
var img = document.createElement('img')
img.src = 'http://domain.com/img.png'
img.onload = function() {
console.log( this.width )
console.log( this.height )
}
这是一个jsfiddle:http://jsfiddle.net/Ralt/VMfVZ/
从技术上讲,这不是jQuery,但我会走以下路线:
var image = new Image(), width, height;
image.src = 'http://whereyourimage.is/heresmyimage.jpg';
width = image.width;
height = image.height;
然后,您可以使用 width
和 height
访问这些值,例如alert('My image is ' + width + ' pixels accross.');
相关文章:
- 使用javascript在Flash中加载外部图像
- 图像可以从源<img src=""/>.TEXT可以在没有javascript的情况下从外部
- 获取图像的平均外部像素颜色
- jQuery对请求图像的外部文本进行查询
- 为在外部单击时关闭的库图像创建预览效果
- node.js表示将外部图像推送到客户端
- 如何使用Node.js显示从http请求到外部API的图像
- 如何在我的html文件中使用外部jquery显示和图像
- 如何获取外部图像并将其编码为客户端 WebApp 上的 base64
- 将用Javascript绘制的外部SVG文件导出为PNG图像
- BX 滑块随图像一起滑动外部字幕
- 在 PHP 中将一个外部图像替换为另一个内部图像
- 如何在Wordpress Fancybox图像上将文本超链接到外部网站
- 加载外部页面时显示图像
- 将变量传递给外部 javascript 文件以通过 onClick 更改图像
- 使图像仅是链接,而不是与叠加相关的外部
- Soundcloud API - 使用 SC.recordupload 从外部 URL 上传曲目图稿的图像
- jQuery:如何获取外部图像的尺寸
- 如何在fabricjs中限制矩形大小调整/移动到图像外部
- 停止裁剪图像外部的裁剪器js