js 自动缩放图片居中
来源:互联网 发布:foxit pdf editor mac 编辑:程序博客网 时间:2024/05/17 22:39
// 缩放图片,imgSrc用户延迟加载图片url
function AutoResizeImage(maxWidth,maxHeight,objImg,imgSrc){
var img = new Image();
img.src = imgSrc || objImg.src;
var hRatio;
var wRatio;
var Ratio = 1;
var w = img.width;
var h = img.height;
wRatio = maxWidth / w;
hRatio = maxHeight / h;
if (maxWidth ==0 && maxHeight==0){
Ratio = 1;
}else if (maxWidth==0){
if (hRatio<1) Ratio = hRatio;
}else if (maxHeight==0){
if (wRatio<1) Ratio = wRatio;
}else if (wRatio<1 || hRatio<1){
Ratio = (wRatio<=hRatio?wRatio:hRatio);
}
if (Ratio<1){
w = w * Ratio;
h = h * Ratio;
}
objImg.style.height = Math.round(h) + "px";
objImg.style.width = Math.round(w) + "px";
if(h < maxHeight) { // 纵向有空余空间
objImg.style.marginTop = Math.round((maxHeight - h) / 2) + "px";
}
if(w < maxWidth) { // 横向有空余空间
objImg.style.marginLeft = Math.round((maxWidth - w) / 2) + "px";
}
if(!!!objImg.src)
objImg.src = imgSrc;
}
function AutoResizeImage(maxWidth,maxHeight,objImg,imgSrc){
var img = new Image();
img.src = imgSrc || objImg.src;
var hRatio;
var wRatio;
var Ratio = 1;
var w = img.width;
var h = img.height;
wRatio = maxWidth / w;
hRatio = maxHeight / h;
if (maxWidth ==0 && maxHeight==0){
Ratio = 1;
}else if (maxWidth==0){
if (hRatio<1) Ratio = hRatio;
}else if (maxHeight==0){
if (wRatio<1) Ratio = wRatio;
}else if (wRatio<1 || hRatio<1){
Ratio = (wRatio<=hRatio?wRatio:hRatio);
}
if (Ratio<1){
w = w * Ratio;
h = h * Ratio;
}
objImg.style.height = Math.round(h) + "px";
objImg.style.width = Math.round(w) + "px";
if(h < maxHeight) { // 纵向有空余空间
objImg.style.marginTop = Math.round((maxHeight - h) / 2) + "px";
}
if(w < maxWidth) { // 横向有空余空间
objImg.style.marginLeft = Math.round((maxWidth - w) / 2) + "px";
}
if(!!!objImg.src)
objImg.src = imgSrc;
}
0 0
- js 自动缩放图片居中
- [转]图片自动缩放 js图片缩放
- javascript图片等比缩放并自动居中
- JS自动缩放页面图片
- 图片的浏览、缩放、拖动和自动居中
- 图片的浏览、缩放、拖动和自动居中
- Android 图片的浏览、缩放、拖动和自动居中
- android 图片的浏览、缩放、拖动和自动居中示例
- Android 图片的浏览、缩放、拖动和自动居中
- Android 图片的浏览、缩放、拖动和自动居中
- 通过Js自动按比例缩放图片
- js控制图片缩放、水平和垂直方向居中对齐
- js控制图片缩放、水平和垂直方向居中对齐
- js控制图片缩放、水平和垂直方向居中对齐
- Android:ImageView图片缩放、居中
- Android:ImageView图片缩放、居中
- 图片缩放并居中显示
- 图片上下左右居中等比缩放
- 漫画:什么是大数据?
- PHP中spl_autoload_register函数的用法
- Using Redis with Node.js
- TextLine 与 buttonMode
- Windows环境下搭建机器学习玩flappy bird(1)
- js 自动缩放图片居中
- 解决Mybatis--java.lang.IllegalArgumentException: Result Maps collection already contains value for XXX
- 归并排序
- Android圆形头像(1)
- Swift3.0 单例模式实现的几种方法-Dispatch_Once
- linux usleep函数参数溢出
- GitHub上完整的iOS-App源码
- 地图动态标绘--B/S应用(二)
- Android 圆形头像(2)