需求是传过来一个图片,根据外层div的大小自动进行缩放效果。

function ShowSecondImg(v) {
var rate, newX, newY,newW, newH = 0; //表示图片相对窗口的缩放比例
var imgW, imgH;
var centerW = $(window).width() - $("#alarmQueue").width() - $("#presInfo").width() - 40; var centerH = $(window).height() - $("#detailedInfor").height()-148;
$("#imgBoxs").find("#second").remove();
var img = $('<img/>', { src: v.First_frame_image_url, "id": "second" });//v.First_frame_image_url img.prependTo('#imgBoxs'); imgW = img.width();
imgH = img.height();
//获取的参数
var rectX= parseInt(v.facerect.x);
var rectY =parseInt(v.facerect.y);
var rectWidth =parseInt(v.facerect.w);
var rectHeight =parseInt(v.facerect.h);
// alert(v.facerect.x); //var i = (centerW / 16 - centerH / 9) > 0 ? 0 : 1;
var i= (centerW/centerH - imgW/imgH ) > 0 ? 0 : 1;
if (i == 1) {
//如果窗口宽不长,高长。以宽为准。
rate = imgW / centerW;
var imgRateH = imgH / rate; //img real rate height;
newX = rectX / rate
newY = rectY / rate;
newW = rectWidth / rate;
newH = rectHeight / rate; $("#imgBox").width(centerW);
$("#imgBox").css("height", "100%");
$("#second").css({ "width": centerW, "height": imgRateH });
}
else if (i == 0) {
//如果窗口宽足够长,高不长。以高为准。
rate = centerH / imgH;
var imgRateW = imgW * rate; //img real rate width;
var leftsideW = (centerW - imgRateW) / 2; //leftside add rightside width;
newX = rectX * rate + leftsideW;
newY = rectY * rate;
newW = rectWidth * rate;
newH = rectHeight * rate; $("#imgBox").height(centerH);
$("#imgBox").css("width", "100%");
$("#second").css({ "width": imgRateW, "height": centerH }); }
// $("#detailedInfor").height($(".container").height()-centerH);
$("#faceDiv").attr('style', 'display:block;top:' + newY + 'px;left:' + newX + 'px;width:' + newW + 'px;height:' + newH + 'px'); }

js实现图片的大小自适应效果的更多相关文章

  1. JS 判断图片尺寸大小,以便页面resize时,动态调整页面元素位置

    ){ clearInterval(global_timename4pool); } } //由于无法判断图片显示完整的时机,只好用定时器来做,计算完成后再关掉定时器. global_timename4 ...

  2. 使用JS实现图片展示瀑布流效果

    不知大家有没有发现,一般的图片展示网站都会使用瀑布流效果,所谓的瀑布流 就是网站内的图片不会一下子全缓存出来,而是等你滚动到一定的距离的时候, 下面的图片才会继续缓存,并且图片也是随机出现的,只是宽度 ...

  3. 用js实现图片的无缝滚动效果

    实现图片的无缝滚动就是要让你的图片集在一定时间里自动切换,那就需要js里的定时器来控制时间. js中关于定时器的方法有两种:setTimeout和setInterval.它们接收的参数是一样的,第一个 ...

  4. js获取图片原始大小

    摘要: 浏览器中显示的图片大小未必是他真实的高和宽,比如像下面这样,我们给他加上宽和高的样式 <img src="IE.png" style="width:25px ...

  5. js 实现图片无限横向滚动效果

    门户网站好多都有产品无线滚动展现的效果: 测试demo1 -- 非无缝滚动(可以看出来从头开始的效果): css样式如下: .box{ width: 1000px; border: 1px solid ...

  6. echarts.js中的图表大小自适应

    echarts的图表,如果父级容器的height/width属性设置为百分比的形式,那么echarts就会warning,且不能正常的生成图表.所以div容器的高度宽度必须指定为px,这设计不知道是为 ...

  7. js插件-图片椭圆轮播效果

    插件效果图: html 代码如下: <div id="container"> <img src="images/cartoon/1.jpg" ...

  8. js限制图片的大小

    <form id="financialForm" action="<%=basePath%>riskcontrol/website/review_bor ...

  9. JS检测图片的大小

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...

随机推荐

  1. Hello Spring Framework——面向切面编程(AOP)

    本文主要参考了Spring官方文档第10章以及第11章和第40章的部分内容.如果要我总结Spring AOP的作用,不妨借鉴文档里的一段话:One of the key components of S ...

  2. 类ExampleA继承Exception,类ExampleB继承ExampleA。 有如下代码片断:

    try { throw new ExampleB("b") } catch(ExampleA e){ System.out.println("ExampleA" ...

  3. Java中excute,excuteUpdate,excuteQuery的区别

    executeQuery(String sql)      执行select语句,它返回的是查询后得到记录集(resultset). executeUpdate(String sql)      执行 ...

  4. Netty系列之Netty高性能之道

    转载自http://www.infoq.com/cn/articles/netty-high-performance 1. 背景 1.1. 惊人的性能数据 最近一个圈内朋友通过私信告诉我,通过使用Ne ...

  5. mysql查询一个小知识点,查询结果是空与查询出错是不一样的

    $conn = new mysqli(....); $sql = ""; $query = $conn->query($sql); 这里,如果查询正常,有数据返回,那么$qu ...

  6. 横版CG场景

    http://www.cgvoo.com/thread-27211-1-1.html <ignore_js_op><ignore_js_op><ignore_js_op& ...

  7. 已禁用对分布式事务管理器(MSDTC)的网络访问的解决方法之一

    C# ASP.NET项目提示上述错误,在代码中使用分布式事务提示添加或修改到数据库的时候.添加数据到数据库时,不会设置实体类的主键字段.

  8. KEEPALIVED

    keepalived 一.原理: 功能:能够自动实现将用户访问的ip转移的方法,故障重启,故障,恢复切换,故障报警 模型:Master/Backup 使用场景:节点少,没有共享存储等等.他只能有一个主 ...

  9. solr 3.5.0 与 tomcat 7.0.5 整合配置

    1.将D:\software\lucene-3.6.0\apache-solr-3.5.0\example\solr下的文件拷贝到要作为服务器的位置(如:D:\lucene\solr\home),可以 ...

  10. iPhone系列设备媒体查询:

    这就引出一个问题,我们在对iPhone设备适配时候,又多出几种情况.iPhone系列设备媒体查询: @media only screen and (min-device-width: 320px){ ...