指定区域的图片自动按比例缩小的js代码(防止页面被图片撑破)

  

复制代码 代码如下:

  <div id=article><img height="800" alt="" width="1280" src="/down/js/images/12498880470.jpg" /></div>

  <script type="text/javascript" >

  //缩放图片到合适大小

  function ResizeImages()

  {

  var myimg,oldwidth,oldheight;

  var maxwidth=550;

  var maxheight=880

  var imgs = document.getElementById('article').getElementsByTagName('img');   //如果你定义的id不是article,请修改此处

  for(i=0;i<imgs.length;i++){

  myimg = imgs[i];

  if(myimg.width > myimg.height)

  {

  if(myimg.width > maxwidth)

  {

  oldwidth = myimg.width;

  myimg.height = myimg.height * (maxwidth/oldwidth);

  myimg.width = maxwidth;

  }

  }else{

  if(myimg.height > maxheight)

  {

  oldheight = myimg.height;

  myimg.width = myimg.width * (maxheight/oldheight);

  myimg.height = maxheight;

  }

  }

  }

  }

  //缩放图片到合适大小

  ResizeImages();

  </script>

  意思就是控制指定区域的的图片大小,要不一些大点的广告图片也会变形。

  管理资源吧用的图片控制代码:

  

复制代码 代码如下:

  function controlImg(ele,w,h){

  var c=ele.getElementsByTagName("img");

  for(var i=0;i<c.length;i++){

  var w0=c[i].clientWidth,h0=c[i].clientHeight;

  var t1=w0/w,t2=h0/h;

  if(t1>1||t2>1||w0>=600){

  c[i].width=Math.floor(w0/(t1>t2?t1:t2));

  c[i].height=Math.floor(h0/(t1>t2?t1:t2));

  if(document.all){

  c[i].outerHTML='<a href="'+c[i].src+'" target="_blank" title="在新窗口查看图片">'+c[i].outerHTML+'</a>'

  }

  else{

  c[i].title="在新窗口打开图片";

  c[i].onclick=function(e){window.open(this.src)}

  }

  }

  }

  }

  ele就是指定的区域,w是最大的宽度,大于这个就会缩小。h是最大的高度。