专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Javascript教程 » 图片等比例缩放:javascript实现图片按比例缩放 »正文

图片等比例缩放:javascript实现图片按比例缩放

来源: 发布时间:星期四, 2009年2月12日 浏览:217次 评论:0


很多人需要代码赶紧都换上吧别让图片把页面撑破了呵呵

function resizeimg(ImgD,iwidth,iheight) { 
     var image= Image
     image.src=ImgD.src; 
     (image.width>0 && image.height>0){ 
        (image.width/image.height>= iwidth/iheight){ 
           (image.width>iwidth){ 
               ImgD.width=iwidth; 
               ImgD.height=(image.height*iwidth)/image.width; 
           }
                  ImgD.width=image.width; 
                  ImgD.height=image.height; 
                } 
               ImgD.alt=image.width+\"×\"+image.height; 
        } 
        
                (image.height>iheight){ 
                       ImgD.height=iheight; 
                       ImgD.width=(image.width*iheight)/image.height; 
                }
                        ImgD.width=image.width; 
                        ImgD.height=image.height; 
                     }  [Page]
                ImgD.alt=image.width+\"×\"+image.height; 
            } 
     ImgD.style.cursor= \"poer\"; //改变鼠标指针 
     ImgD.onclick = function { window.open(this.src);} //点击打开大图片 
     (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; 
           (zoom> 0) this.style.zoom = zoom + \"%\"; 
           false; 
      } 
     }  { //如果不是IE 
            ImgD.title = \"点击图片可在新窗口打开\"; 
         } 
    } 
}在需要实现等比缩放图片上加上onload语句图片装载时化大小 
具体实现代码如下:

<img name=\"\" src=\"\" onload=\"javascript:resizeimg(this,100,200)\">

0

相关文章

读者评论

  • 1icefish(219.156.124.*) 发布于:星期二, 2008年11月11日
    代码错误不少啊

发表评论

  • 昵称:
  • 内容: