JS等比例缩放图片
2011-05-23 22:31
218 查看
function DrawImage(ImgD, iwidth, iheight) {
var image = new Image();
image.src = ImgD.src;
if (image.width > 0 && image.height > 0) {
flag = true;
if (image.width / image.height >= iwidth / iheight) {
if (image.width > iwidth) {
ImgD.width = iwidth;
ImgD.height = image.height * iwidth / image.width;
} else {
ImgD.width = image.width;
ImgD.height = image.height;
}
ImgD.alt = image.width + "×" + image.height;
}
else {
if (image.height > iheight) {
ImgD.height = iheight;
ImgD.width = image.width * iheight / image.height;
} else {
ImgD.width = image.width;
ImgD.height = image.height;
}
ImgD.alt = image.width + "×" + image.height;
}
}
} 优化以后
function DrawImage(ImgD, iwidth, iheight) {
var image = new Image();
image.src = ImgD.src;
if (image.width > 0 && image.height > 0)
{
if (image.width / iwidth > image.height / iheight) {
if (image.width > iwidth)
{
ImgD.width = iwidth;
ImgD.height = image.height * iwidth / image.width;
} else
{
ImgD.width = iwidth;
ImgD.height = image.height * image.width / iwidth;
}
}
else if(image.width / iwidth < image.height / iheight)
{
if (image.height > iheight)
{
ImgD.height = iheight;
ImgD.width = image.width * iheight / image.height;
} else
{
ImgD.height= image.width;
ImgD.width = image.width * image.height / iheight
}
}else
{
ImgD.width = iwidth;
ImgD.height = iheight;
}
}
}
var image = new Image();
image.src = ImgD.src;
if (image.width > 0 && image.height > 0) {
flag = true;
if (image.width / image.height >= iwidth / iheight) {
if (image.width > iwidth) {
ImgD.width = iwidth;
ImgD.height = image.height * iwidth / image.width;
} else {
ImgD.width = image.width;
ImgD.height = image.height;
}
ImgD.alt = image.width + "×" + image.height;
}
else {
if (image.height > iheight) {
ImgD.height = iheight;
ImgD.width = image.width * iheight / image.height;
} else {
ImgD.width = image.width;
ImgD.height = image.height;
}
ImgD.alt = image.width + "×" + image.height;
}
}
} 优化以后
function DrawImage(ImgD, iwidth, iheight) {
var image = new Image();
image.src = ImgD.src;
if (image.width > 0 && image.height > 0)
{
if (image.width / iwidth > image.height / iheight) {
if (image.width > iwidth)
{
ImgD.width = iwidth;
ImgD.height = image.height * iwidth / image.width;
} else
{
ImgD.width = iwidth;
ImgD.height = image.height * image.width / iwidth;
}
}
else if(image.width / iwidth < image.height / iheight)
{
if (image.height > iheight)
{
ImgD.height = iheight;
ImgD.width = image.width * iheight / image.height;
} else
{
ImgD.height= image.width;
ImgD.width = image.width * image.height / iheight
}
}else
{
ImgD.width = iwidth;
ImgD.height = iheight;
}
}
}
相关文章推荐
- 关于图片按比例自适应缩放的js代码
- JS控制图片显示的大小(图片等比例缩放)
- 【JS控制图片显示的大小(图片等比例缩放)】
- JS实现按比例缩放图片的方法(附C#版代码)
- JS控制图片显示的大小(图片等比例缩放)
- JS控制图片显示的大小(图片等比例缩放)
- js 等比例缩放图片
- js等比例缩放图片
- JS控制图片显示的大小(图片等比例缩放)
- JS控制图片显示的大小(图片等比例缩放)
- JS控制图片显示的大小(图片等比例缩放)
- 纯JS实现图片预览与等比例缩放和居中
- js按比例缩放图片尺寸
- JS控制图片显示的大小(图片等比例缩放)
- JS控制图片按比例缩放并不超过一定宽度和高度(居中对齐)
- js等比例缩放图片
- js 小小jquery等比例缩放图片效果
- JS按比例缩放图片
- js同比例缩放图片
- JS 等比例缩放图片