/*
重新按比例设置 页面上对应图片的长和高,
*/
function resetImgSize(id,imgWidth,imgHeight,posWidth,posHeight) {
var width = 0;
var height = 0;
// 按比例缩小图片的算法
if(imgWidth > imgHeight) {
if(imgWidth > posWidth) {
width = posWidth;
height = imgHeight/imgWidth * width; }else {
width = imgWidth;
height = imgHeight;
}
}else {
if(imgHeight > posHeight) {
height = posHeight;
width = (imgWidth/imgHeight) * height;
}else {
width = imgWidth;
height = imgHeight;
}
}
width = Math.floor(width);
height = Math.floor(height);
$('#'+id).attr('width',width);
$('#'+id).attr('height',height);
$('#'+id).css('padding-left',(posWidth-width)/2);
$('#'+id).css('padding-top',(posHeight-height)/2); } /*
获取图片尺寸
imgURL 图片加载地址
posId 图片位置id
posWidth 放在图片位置的width
posHeight 放在图片位置的height
*/ function getImgSize(imgURL,posId,posWidth,posHeight) { var img = new Image(); img.src = imgURL+'?'+Math.random();
var width = 0;
var height = 0; var check = function(){ if(img.width > 0) { clearInterval(set); resetImgSize(posId,img.width,img.height,posWidth,posHeight);
}
}; var set = setInterval(check,40); img.onload = function(){ clearInterval(set);
}; } $(function(){
$('.resetsize').each(function(){
getImgSize(this.src,this.id,168,168);
});
});

js 完成对图片的等比例缩放的方法的更多相关文章

  1. js实现图片的等比例缩放

      js实现图片的等比例缩放 CreateTime--2018年3月6日14:04:18 Author:Marydon 1.代码展示 /** * 图片按宽高比例进行自动缩放 * @param ImgO ...

  2. CSS实现图片快速等比例缩放,效果佳

    初学者在实现图片等比例缩放,通常会使用js编写逻辑来控制高或宽,达到自动缩放的效果. 这里提供一种纯CSS的图片缩放功能,请看代码: <style type="text/css&quo ...

  3. android项目 之 记事本(12) ----- 图片的等比例缩放及给图片加入边框

    本文是自己学习所做笔记.欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 在Android的UI开发中常常会遇到图片的缩放,就比方记事本,如今的图片都比較 ...

  4. Qt图片按原比例缩放

    1.选择图片 QString strFilePath = QFileDialog::getOpenFileName(this, tr("Select file"), QStanda ...

  5. java读取jpg图片旋转按比例缩放

    //入口 public static BufferedImage constructHeatWheelView(int pageWidth, int pageHeight, DoubleHolder ...

  6. PHP对图片按照一定比例缩放并生成图片文件

    list($width, $height)=getimagesize($filename);//缩放比例$per=round(400/$width,3); $n_w=$width*$per;$n_h= ...

  7. jQuery制作图片的等比例缩放

    1资料的排版 2.html代码 <body> <div class="BB"><img src="dw.jpg" alt=&quo ...

  8. iOS 网络/本地 图片 按自定义比例缩放 不失真 方法

    我尝试了很多种方法,终于,设计了一个方法,能按自己规定的大小压缩 还没失真 如果以后不好用 我再升级 分享给大家: + (CGRect )scaleImage:(UIImage *)image toS ...

  9. CSS图片Img等比例缩放且居中显示

    常用来做图片放大显示的遮罩层imgScale HTML <div id="imgScale" > <img src=""> </d ...

随机推荐

  1. sublime text 2 中文乱码解决办法

    sublime text 2是一款非常优秀的跨平台文本及源代码编辑器,本人非常喜欢,但是不支持GB2312和GBK编码在某些时候比较麻烦.可以通过向sublime text 中添加编码类型转换包(比如 ...

  2. 在网页中使用H1标记的须注意的事项

    H1标签是网站排名非常重要的一个因素,因此我们一定要正确使用它. 本文为你介绍H1标签使用的七大注意事项: 1.每个页面都应该有H1标签,H1标签是每个网页不可缺少的要素. 2.使用H1标签的内容应该 ...

  3. ASP.NET中Server对象的几个方法

    HtmlDecode 已重载. 对已被编码以消除无效 HTML 字符的字符串进行解码.HtmlEncode 已重载. 对要在浏览器中显示的字符串进行编码.MapPath 返回与 Web 服务器上的指定 ...

  4. Python生态环境简介[转]

    Python生态环境简介 作者: Mir Nazim 原文: Python Ecosystem - An Introduction 译者: dccrazyboy  原译: Python生态环境简介 当 ...

  5. php 实现多线程

    通过php的Socket方式实现php程序的多线程.php本身是不支持多线程的,那么如何在php中实现多线程呢?可以想一下,WEB服务器本身都是支持多线程的.每一个访问者,当访问WEB页面的时候,都将 ...

  6. GOOGLE 离线完整安装包下载地址

    https://support.google.com/chrome/answer/126299?hl=zh-Hans 官方链接介绍 https://www.google.com/chrome/brow ...

  7. Git - Tutorial [Lars Vogel]

    From: http://www.vogella.com/tutorials/Git/article.html Git - Tutorial Lars Vogel Version 5.6 Copyri ...

  8. ios category

    https://github.com/shaojiankui/IOS-Categories

  9. java的变量

    什么是变量? 在计算机中用来存储信息,通过声明语句来指明存储位置和所需空间. 变量的声明方法及赋值 分号:语句结束标志             赋值号:将=右边的值赋给左边的变量 变量有哪些数据类型? ...

  10. 批处理判断是否存在文件,存在则运行另外一个bat文件

    现在需求如下: 使用bat文件判断是否存在ktr文件,存在则运行pan.bat,执行kettle脚本. 代码如下: @echo off @title 批处理判断文件夹是否存在 cd /d F: rem ...