---
title: "图片按比例厄缩略处理js"
author: "celine_q"
date: "2010-04-21T09:32:00Z"
tags: []
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2010-04-21T09:32:45Z"
id: 649021
url: "https://celineq.iteye.com/blog/649021"
---

在jsp页面body元素中 onload 方法调用

 

function resizeImgs(imageName,imgWidth,imgHeight){

    var imgs=document.getElementsByName(imageName);

    var currentWidth,currentHeight;

    for(var i=0;i&lt;imgs.length;i++){

       //设置图片宽度界限

       var MaxWidth=imgWidth;

       //设置图片高度界限

       var MaxHeight=imgHeight;

       //设置高宽比

       var HeightWidth=imgs\[i].offsetHeight/imgs\[i].offsetWidth;

       //设置宽高比

       var WidthHeight=imgs\[i].offsetWidth/imgs\[i].offsetHeight;

       if(imgs\[i].offsetWidth&gt;MaxWidth){

           imgs\[i].width=MaxWidth;

           imgs\[i].height=MaxWidth\*HeightWidth;

       }

       if(imgs\[i].offsetHeight&gt;MaxHeight){

           imgs\[i].height=MaxHeight;

           imgs\[i].width=MaxHeight\*WidthHeight;

       }

       imgs\[i].style.marginTop = (imgHeight-imgs\[i].height)/2 + "px";

    }

}