js 完成对图片的等比例缩放的方法
/*
重新按比例设置 页面上对应图片的长和高,
*/
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 完成对图片的等比例缩放的方法的更多相关文章
- js实现图片的等比例缩放
js实现图片的等比例缩放 CreateTime--2018年3月6日14:04:18 Author:Marydon 1.代码展示 /** * 图片按宽高比例进行自动缩放 * @param ImgO ...
- CSS实现图片快速等比例缩放,效果佳
初学者在实现图片等比例缩放,通常会使用js编写逻辑来控制高或宽,达到自动缩放的效果. 这里提供一种纯CSS的图片缩放功能,请看代码: <style type="text/css&quo ...
- android项目 之 记事本(12) ----- 图片的等比例缩放及给图片加入边框
本文是自己学习所做笔记.欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 在Android的UI开发中常常会遇到图片的缩放,就比方记事本,如今的图片都比較 ...
- Qt图片按原比例缩放
1.选择图片 QString strFilePath = QFileDialog::getOpenFileName(this, tr("Select file"), QStanda ...
- java读取jpg图片旋转按比例缩放
//入口 public static BufferedImage constructHeatWheelView(int pageWidth, int pageHeight, DoubleHolder ...
- PHP对图片按照一定比例缩放并生成图片文件
list($width, $height)=getimagesize($filename);//缩放比例$per=round(400/$width,3); $n_w=$width*$per;$n_h= ...
- jQuery制作图片的等比例缩放
1资料的排版 2.html代码 <body> <div class="BB"><img src="dw.jpg" alt=&quo ...
- iOS 网络/本地 图片 按自定义比例缩放 不失真 方法
我尝试了很多种方法,终于,设计了一个方法,能按自己规定的大小压缩 还没失真 如果以后不好用 我再升级 分享给大家: + (CGRect )scaleImage:(UIImage *)image toS ...
- CSS图片Img等比例缩放且居中显示
常用来做图片放大显示的遮罩层imgScale HTML <div id="imgScale" > <img src=""> </d ...
随机推荐
- iOS 支付宝支付集成获取私钥
http://doc.open.alipay.com/doc2/apiList?docType=4 登录到支付宝开放平台,下载相关支付宝支付的demo.解压出来有3个文件夹.(服务端demo,客户端 ...
- 关于fork函数中的内存复制和共享
原来刚刚开始做linux下面的多进程编程的时候,对于下面这段代码感到很奇怪, #include<unistd.h> #include<stdio.h> #include< ...
- SQLServer存储过程入门
1.创建一个返回结果集的存储过程 create procedure firstpro As begin select * from dbo.Person End 执行: execute dbo.fir ...
- zend studio导入thinkphp的乱码问题
刚刚导入thinkphp有乱码还有错误怎么办? windows -> preference -> Work space -> text file encodeing设置为 UTF-8 ...
- synchronized的重入
/** * synchronized的重入 * */ public class SyncDubbo1 { public synchronized void method1(){ System.out. ...
- 使用java8的lambda将list转为map(转)
常用方式 代码如下: public Map<Long, String> getIdNameMap(List<Account> accounts) { return accoun ...
- AsyncTask的简单使用
package com.zzw.life; import android.app.Activity; import android.os.AsyncTask; import android.os.Bu ...
- 删:[CentOS 7] 安装nginx
下载对应当前系统版本的nginx包(package) # wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-cent ...
- 对于java反射的理解
java中的反射是一种强大的工具,它能够创建灵活的代码,这些代码可以在运行时装配,无序在组件之间进行链接. 反射允许在编写与执行时,使程序代码能够接入装载到JVM的类的内部信息,而不是源代码中选定的类 ...
- xtrabackup之Innobackupex全备恢复
一.当前环境 [mysql@hadoop1 ~]$ mysql --defaults-/my.cnf -uroot -p123456 -P3306 mysql> show variables l ...