五款js 图片等比例缩放效果

<script type="text/网页特效">
<!--
//图片按比例缩放---素材图片页面控制
var flag=false;
function newdrawimage(imgd){
 var image=new image();
 var iwidth = 200;  //定义允许图片宽度
 var iheight = 150;  //定义允许图片高度
 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;
  }
 }

//-->
</script>

<img src="admin/uploadpic/2009811522257007705.jpg" width="194" height="130" border="0"  onload="newdrawimage(this)"/>

方法一:在图片加载完毕后用onload句柄触发执行脚本,重设图片的宽度为图片宽度和300两者的最小值。
代码示例: <img src="demo.gif" onload="if(this.width>300)this.width=300">
或者

<img src="demo.gif" onload="this.width=math.min(this.width,300)">

方法二:按比例缩小。只需要把js加在head之间,再在body处加入onload="resizeimages();"代码。

<script language="javascript">
<!--
function resizeimages()
{
var myimg,oldwidth;
var maxwidth=180; //缩放系数
for(i=0;i <document.images.length;i++){
myimg = document.images[i];
if(myimg.width > maxwidth)
{
oldwidth = myimg.width;
myimg.width = maxwidth;
myimg.height = myimg.height * (maxwidth/oldwidth);
}
}
}
//-->
</script>

<script language="javascript">
<!--
//图片按比例缩放
var flag=false;
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;
}
}
}
//-->
</script>

调用:<img src="images/toplogo.gif" onload="javascript:drawimage(this,100,100)">

方法三

<script language="javascript">
<!--
//图片按比例缩放
var flag=false;
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;
}
}
}
//-->
</script>

调用

:<img src="images/toplogo.gif" onload="javascript:drawimage(this,100,100)">

方法四

<script type="text/javascript">
function resizeimg(imgd,iwidth,iheight) {
     var image=new image();
     image.src=imgd.src;
     if(image.width>0 && image.height>0){
        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;
            }
     imgd.style.cursor= "pointer"; //改变鼠标指针
     imgd.onclick = function() { window.open(this.src);} //点击打开大图片
    if (navigator.useragent.tolowercase().indexof("ie") > -1) { //判断浏览器,如果是ie
      imgd.title = "请使用鼠标滚轮缩放图片,点击图片可在新窗口打开";
      imgd.onmousewheel = function img_zoom() //滚轮缩放
      {
          var zoom = parseint(this.style.zoom, 10) || 100;
          zoom += event.wheeldelta / 12;
          if (zoom> 0) this.style.zoom = zoom + "%";
          return false;
      }
     } else { //如果不是ie
            imgd.title = "点击图片可在新窗口打开";
         }
    }
}
</script>

 

<img name="" src="upload/<%=rs("picfilename")%>" alt="<%=rs("title")%>" style="border:1px #999 solid;" onload="javascript:resizeimg(this,150,140)">

 

时间: 2024-08-31 07:39:45

五款js 图片等比例缩放效果的相关文章

几款js图片等比例缩放效果代码

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.111cn.net/ 1999/xhtml"> <head> <meta http-equiv="cont

两款js图片等比例缩放(兼容IE6,IE7,FF)

 代码如下 复制代码 <script language="网页特效" type="text/javascript"> <!-- function imgauto(i){//你必须给图片提前设定初始宽度和高度,建议直接就是最大宽度和高度  var maxw=250; //定义图片显示的最大宽度  var maxh=250; //定义图片显示的最大高度  var o=new image();o.src=i.src;var w=o.width;var h

JS图片等比例缩放方法完整示例_javascript技巧

本文实例讲述了JS图片等比例缩放方法.分享给大家供大家参考,具体如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>

js图片等比例缩放

js图片等比例缩放 /使用onload=resizeImage(this,长,宽); function resizeImage(img,w,h) {     var image=new Image();     image.src = img.src;     if(image.width > 0 && image.height > 0)     {         if(image.width / image.height >= w/h)         {      

js图片按比例缩放代码

js图片按比例缩放代码 <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>jjs图片按比例缩放代码</title> <script language="javascript教程">  //图片按比例缩放 var flag = false; function DrawIm

js 图片按比例缩放

<!-- //图片按比例缩放 var flag=false; function DrawImage(ImgD){  var image=new Image();  var iwidth = 150;  //定义允许图片宽度  var iheight = 150;  //定义允许图片高度  image.src=ImgD.src;  if(image.width>0 && image.height>0){  flag=true;  if(image.width/image.h

js图片等比例缩放程序代码

例1  代码如下 复制代码 <script type="text/javascript">  function changeImg(objImg)  {      var most = 690;        //设置最大宽度      if(objImg.width > most)      {          var scaling = 1-(objImg.width-most)/objImg.width;             //计算缩小比例       

四款css 图片按比例缩放实例(兼容ie6,7,firefox)

使用max-width,max-height:或者min-width,min-height的css属性即可.如:  代码如下 复制代码 img{max-width:100px;max-height:100px;} img{min-width:100px;min-height:100px;} 对于ie6及其以下版本的浏览器,则可以利用其支持的expression属性,在css code中嵌入网页特效 code 来实现图片的缩放 .thumbimage {max-width: 100px;max-h

js 图片按比例缩放代码

这是一款可以根据图片的大小自行按比便进行放大或缩小效果的简单js代码哦. <script> function resizepic(thispic) { if(thispic.width>570) thispic.width=570; } function bbimg(o) {   var zoom=parseInt(o.style.zoom, 10)||100;   zoom+=event.wheelDelta/12;   if (zoom>0) o.style.zoom=zoom