网页中图片大小自动调整三种方法

1.鼠标滚动图片大小调整

<body>
<script LANGUAGE=”JAVASCRIPT”>
function bbimgo){
   var zoom=parseInto.style.zoom, 10)||100;

zoom+=event.wheelDelta/12;

if zoom>0)

 o.style.zoom=zoom+’%’;

   return false;
}
</script> 
<img src=”123.jpg” onload=”javascript:ifthis.width>screen.width-screen.width/2) this.style.width=screen.width-screen.width/2″ onmousewheel=”return bbimgthis)” > 
</body>

2.图片自动缩小到固定大小

<body onload=”DrawImagetheimg)”>
<script language=”JavaScript”>
<!–
//图片按比例缩放
var flag=false;
function DrawImageImgD){
  var image=new Image);
  var iwidth = 320;  //定义允许图片宽度
  var iheight = 180;  //定义允许图片高度
  image.src=ImgD.src;
  ifimage.width>0 && image.height>0){
   flag=true;
   ifimage.width/image.height>= iwidth/iheight){
    ifimage.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{
    ifimage.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;
    }
   }

//–>
</script>
<img src=”123.jpg” width=”1024″ height=”768″ name=”theimg”>
</body> 

3.滚轮图片缩放的代码

<script language=”javascript”>
var count = 10;
function resizeimgoImage)

count = Countingcount);
ResizeoImage,count);
return false;
}
function Countingnewzoom){ 
if event.wheelDelta >= 120)
newzoom++;
else if event.wheelDelta <= -120)
newzoom–;
if newzoom<2) newzoom=2; ////只允许缩小到20%
if newzoom>50) newzoom=50; ////只允许放大到500%
return newzoom; 
}
function ResizeoImage,newzoom){ 
oImage.style.zoom = newzoom + ‘0%’; 
count=newzoom;
}
</script>

然后在<img src=””>中加入
onDblClick=”return Resizethis,10);return false;” 
onmousewheel=”return resizeimgthis)”

千人.NET交流群:18362376,因为有你,代码变得更简单,加群请输入cnblogs

Published by

风君子

独自遨游何稽首 揭天掀地慰生平