九米哥软件开发九米哥软件开发
www.9mge.com
开心网外挂,QQ停车外挂,软件开发,加密与解密,SEO学习,电子商务网站开发,编程语言学习,软件开发创意分享,软件发布,外挂源码
« 开心网大师组件介绍IE8在地址框中输入:www.b就出错! »

js控制图片自动缩小(在IE8中的表现)

不说那么多,在没有出现IE8时,一直都在用的图片缩小JS函数,从没出过错。
//此函数在网上到处可见,以前也一直都用,在火狐和谷歌浏览器中没有问题。
function DrawImage(ImgD,width,height){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
//alert(image.width);
  //flag=true;
  if(image.width/image.height>= width/height){
   if(image.width>width){
    ImgD.width=width;
    ImgD.height=(image.height*height)/image.width;
   }
   else
   {
    ImgD.width=image.width;
    ImgD.height=image.height;
   }
   /*ImgD.alt="bigpic"  */
  }
  else{
   if(image.height>height){
    ImgD.height=height;
    ImgD.width=(image.width*height)/image.height;
   }
   else{
    ImgD.width=image.width;
    ImgD.height=image.height;
   }
  }
}
}

这是在一次偶然情况下,发现一张同样的图片,IE8却出现不同的缩小比例,差点让我在给客户做演示时出丑,重新写过此JS更简洁。
但这并不是说上面的那个函数有错,而是它在IE8上和表格套在一起出错问题。但下面这个不会出现。

//自动缩小图片,ImgD为img,width为要缩小到的宽度,height为要缩小到的高度。按比例缩放。
//调用示例<img src="http://www.9mge.com/image/common/myselfpp.jpg"  onload="DrawImage(this,50,50);">
function DrawImage(ImgD, width, height) {
    var image = new Image();
    image.src = ImgD.src;
    //当存在图片时,且宽度大于要缩小的或高度大于要缩小的
    if (image.width > 0 && image.height > 0 && (image.width > width || image.height > height)) {
        if (image.width > image.height) {//宽大于高时,按宽的比例缩
            ImgD.width = width;
            ImgD.height = (width / ImgD.width * ImgD.height);
        } else {//高大于宽时,按高的比例缩
            ImgD.height = height;
            ImgD.width = (height / ImgD.height * ImgD.width);
        }
    }
}
 

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By www.9mge.com1.8 Arwen Build 90619

Copyright 9mge.com 版权所有