Javascript Image()返回错误的宽度和高度
Javascript Image() return wrong Width and Height
我正在使用交叉幻灯片Jquery插件,我有一个疑问。
当我尝试加载尺寸为1176 x 1168的图像时,.width
属性返回600,.height
属性返回595。
为什么不返回图像的当前尺寸?
插件代码:
// first preload all the images, while getting their actual width and height
(function (proceed) {
var n_loaded = 0;
function loop(i, img) {
// for (i = 0; i < plan.length; i++) but with independent var i, img (for the closures)
img.onload = function (e) {
n_loaded++;
// Here are my doubt
plan[i].width = img.width; // returning 600
plan[i].height = img.height; // returning 595
if (n_loaded == plan.length)
proceed();
}
img.src = plan[i].src;
if (i + 1 < plan.length)
loop(i + 1, new Image());
}
loop(0, new Image());
})(function () { // then proceed ...
你需要修改一下你的代码(主要是loop
函数),然后再试一次,像这样:
function loop(i, img) {
img.src = plan[i].src;
img.onload = function (e) {
n_loaded++;
plan[i].width = this.width; // Check the width now
plan[i].height = this.height; // Check the height now
if (n_loaded == plan.length) proceed();
}
if (i + 1 < plan.length) loop(i + 1, new Image());
}
简单演示
你试过了吗?
var img = document.getElementById('imageid');
var width = img.clientWidth;
var height = img.clientHeight;
相关文章:
- 文档就绪提供了错误的选择器高度
- Jqx图表给出错误错误:
属性高度=“-1”的负值无效 - Jquery高度返回错误值
- CodeMirror在粘贴自定义css线条高度时呈现错误
- 触发方向更改事件时获得错误的屏幕高度
- Android Webview onLoad返回错误的高度
- 什么'用这个代码查找高度英寸是错误的
- Galleria致命错误:无法从CSS/Multiple Galleria和Display中提取舞台高度:none;
- Firefox SDK:包括jquery.flot在内的问题,错误显示维度高度为空
- 谷歌浏览器错误地计算了元素的高度
- 带有样式显示的 Div:表格的高度错误
- 水星编辑器 0.9 - 内容 iFrame 位于编辑器控件后面,高度错误
- 加载 HTML 网站时,安卓上的窗口高度错误
- 在滚动野生动物园错误上增加元素的高度
- ajax表单提交后iScroll4高度错误.高度是表单提交前的原始高度
- jquery最大高度和最大宽度返回错误的值
- 镀铬和FF错误的窗户高度
- jQuery美元(窗口).高度错误的动态添加内容
- 谷歌应用程序脚本setValues()高度错误
- HTML:Base64 图像显示/复制高度错误