用JavaScript获取图像的真实宽度和高度?(Safari/Chrome)我正在创建一个jQuery插件。如何在Safari中使用Javascript获得真实的图像宽度和高度?下面是Firefox 3、IE7和Opera 9的作品:var pic = $("img")// need to remove these in of case img-element has set width and heightpic.removeAttr("width");
pic.removeAttr("height");var pic_real_width = pic.width();var pic_real_height = pic.height();但是在像Safari和GoogleChrome这样的Webkit浏览器中,值是0。
3 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
var img = $("img")[0]; // Get my img elemvar pic_real_width, pic_real_height;$("<img/>") // Make in memory copy of image to avoid css issues .attr("src", $(img).attr("src")) .load(function() { pic_real_width = this.width; // Note: $(this).width() will not pic_real_height = this.height; // work for in memory images. });
naturalHeight
naturalWidth
慕的地8271018
TA贡献1796条经验 获得超4个赞
function getOriginalWidthOfImg(img_element) { var t = new Image(); t.src = (img_element.getAttribute ? img_element.getAttribute("src") : false) || img_element.src; return t.width;}
添加回答
举报
0/150
提交
取消