设置div正方形--快速获得图片真实的宽度和高度
2015-06-16 14:36
232 查看
$(window).load(function() {
//获取div的宽度,设置div正方形
var width1=document.getElementById("div1").offsetWidth;
$("#div1").css("height",width1).find("img").show();
//获取图片的真实宽高
var _w=parseInt($(window).width());
$(".con02 img").each(function(){
var img = $(this);
var realWidth;//真实的宽度
var realHeight;//真实的高度
$("<img/>").attr("src", $(img).attr("src")).load(function() {
realWidth = this.width;
realHeight = this.height;
if(realWidth>=_w){
$(img).css("width","100%").css("height","auto");
}
else{
$(img).css("width",realWidth+'px').css("height",realHeight+'px');
}
})
})
})
网上发现一个定时检测图片宽高的方法,效率非常高。具体代码如下:
转载链接:http://www.111cn.net/wy/js-ajax/68065.htm
//获取div的宽度,设置div正方形
var width1=document.getElementById("div1").offsetWidth;
$("#div1").css("height",width1).find("img").show();
//获取图片的真实宽高
var _w=parseInt($(window).width());
$(".con02 img").each(function(){
var img = $(this);
var realWidth;//真实的宽度
var realHeight;//真实的高度
$("<img/>").attr("src", $(img).attr("src")).load(function() {
realWidth = this.width;
realHeight = this.height;
if(realWidth>=_w){
$(img).css("width","100%").css("height","auto");
}
else{
$(img).css("width",realWidth+'px').css("height",realHeight+'px');
}
})
})
})
网上发现一个定时检测图片宽高的方法,效率非常高。具体代码如下:
代码如下 | 复制代码 |
// 记录当前时间戳 var start_time = new Date().getTime(); // 图片地址 var img_url = 'img/bdlogo.png?'+start_time; // 创建对象 var img = new Image(); // 改变图片的src img.src = img_url; // 定时执行获取宽高 var check = function(){ // 只要任何一方大于0 // 表示已经服务器已经返回宽高 if(img.width>0 || img.height>0){ var diff = new Date().getTime() - start_time; document.body.innerHTML += ' from:check : width:'+img.width+',height:'+img.height+', time:'+diff+'ms '; clearInterval(set); } }; var set = setInterval(check,40); |
相关文章推荐
- commons logging的getFactory()方法
- 如何查找订单提示VPRS VE217 数量/值确定时出错
- 并发编程
- NX Open 切削层加载
- Navicat for MySQL出现1030-Got error 28 from storage engine错误
- linux学习 命令ll后字段的解释(转)
- js中Array对象方法详解
- MAC下Eclipse的常用快捷键
- 绘图:Matplotlib
- Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
- 回车和换行的区别??
- 《开源框架那点事儿14》:教计算机程序解数学题
- Storyboard中的UIScrollView使用自动布局,使其能够滚动
- 解码图像保存单帧
- C#使用memcached实现缓存
- word文档打钩记录快捷键
- 如何从H264原始数据SPS里面得到视频的长宽
- 如何删除不必要的开票的出库单?
- 老板有事找你——如何在HiWork中进行私聊
- Vmware vSphere 5.0系列教程之六 虚拟机及主机配置文件的创建和模板部署