一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

JavaScript获取图片的真实尺寸大小示例

时间:2014-10-01 编辑:简简单单 来源:一聚教程网

最简单的基本jquery插件获取教程

 代码如下 复制代码

var img = $("#img_id"); // Get my img elem
var pic_real_width, pic_real_height;
$("") // 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.
 });

例子,如果你觉得jquery插件包大了,我们可以使用js来实现

 代码如下 复制代码

function adapt(){
var tableWidth = $("#imgTable").width(); //表格宽度
var img = new Image();
img.src =$('#imgs').attr("src") ;
var imgWidth = img.width; //图片实际宽度
if(imgWidth $('#imgs').attr("style","width: auto");
}else{
$('#imgs').attr("style","width: 100%");
}
}

css代码

如果你想获取远程机器图片我们可以用下面代码。

 代码如下 复制代码

JavaScript获取远程<a href="/tags.php/%CD%BC%C6%AC%B4%F3%D0%A1/" target="_blank">图片大小</a>


热门栏目