一只牛の窝

生命只是在演绎着自己的精彩,分享才是最大的快乐……

« 利用JS把URL地址栏%20、%55之类编码转成中文字符安静JS弹窗代码Cookie控制点击页面必弹出来后投窗口 »

JS控制大图片大小与CSS控制大图片大小代码及范例

或许大家做网页都试过这样的经历,因为网页上的图片太大了打网页撑得变形,真是非常郁闷的一件事,我就前段时间出现了这样的事情,在网上找了几个可用的代码在这里发布同大家分享一下。

一、JS按比例缩小大图片方法,在与之间加入下面代码:

<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>

调用方法:

onload=;DrawImage(this,750,450);


JS控制大图片大小例子:

        



二、CSS按比例缩小大图片方法,在与之间加入下面代码:

<style type="text/css">
.thumbImage {max-width: 750px;max-height: 450px;} /* for Firefox & IE7 */ 
* html .thumbImage { width: expression(this.width > 750 & this.height < this.width ? 750: true); height: expression(this.height > 450 ? 450: true); }
</style>

调用方法:

class="thumbImage"

CSS控制大图片大小例子(一):

        



三、还有一种更简单的CSS代码,在与之间加入下面代码无需调用控制页面所有图片:

<style type="text/css">
Img {max-width: 750px;max-height: 450px;} /* for Firefox & IE7 */ 
Img { width: expression(this.width > 750 & this.height < this.width ? 750: true); height: expression(this.height > 450 ? 450: true); }
</style>

CSS控制大图片大小例子(二):

        



上面发了3种方法可以控制大图片自动缩小,至于那一种比较合适自己决定吧,有问题直接回复本贴。


原创文章如转载,请注明:转载自 阳春一月 [ http://blog.ycjan.com/ ]

本文链接地址:http://blog.ycjan.com/javascript/20090526181.html

发表评论:

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

日历

Search

最近发表

Powered By Z-Blog 1.8 Arwen Build 81206

Copyright ycjan.com 粤ICP备08002314号