问题来了,如何用js获取图片原始大小?
摘要:
浏览器中显示的图片大小未必是他真实的高和宽,比如像下面这样,我们给他加上宽和高的样式。
<img src="IE.png" style="width:25px;height:25px;">
这样在浏览器中显示的大小就是25px。那么我们如何获取图片的真实大小呢?
下面的代码就实现了这个功能:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<img src="IE.png" id="image" style="width:25px;height:25px;">
<script>
// 设置延时保证图片加载完成
setTimeout(function() {
var
real_width,
real_height,
_im = document.getElementById('image'),
im = document.createElement('img');
im.src = _im.src,
real_width = im.width,
real_height = im.height;
alert(real_width+'\n'+real_height);
},500);
</script>
</body>
</html>
- javascript nodetype 3
- javascript 衠c
- javascript 输出变量值
- 在线学习javascript
- 怎么禁止javascript
- javascript changerecrooms
- javascript this function
- javascript goto function
- javascript if not equal
- javascript write
- javascript 对象赋值
- ie 启用javascript
- javascript 次方
- javascript 转义
- javascript alert 超链接
- a href javascript
- javascript good parts pdf
- javascript delay timer
- pdf javascript viewer
- javascript array function
注意:上面代码本人在IE7+和chrome上都测试过了,因为没有装IE6,所以没法测试。