Jquery中文网 www.jquerycn.cn
Jquery中文网 >  CSS教程  >  经典实例  >  正文 图片自适应大小

图片自适应大小

发布时间:2020-05-22   编辑:www.jquerycn.cn
jquery中文网为您提供图片自适应大小等资源,欢迎您收藏本站,我们将为您提供最新的图片自适应大小资源
 代码如下 复制代码

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var flag=false;
function DrawImage(ImgD,imageWidth,imageHeight) {
   var image=new Image();
   image.src=ImgD.src;
   if(image.width>0 && image.height>0){
    flag=true;
    if(image.width/image.height>= imageWidth/imageHeight){
     if(image.width>imageWidth){
     ImgD.width=imageWidth;
     ImgD.height=(image.height*imageWidth)/image.width;
     }else{
     ImgD.width=image.width;
     ImgD.height=image.height;
     }
     }
    else{
     if(image.height>imageHeight){
     ImgD.height=imageHeight;
     ImgD.width=(image.width*imageHeight)/image.height;
     }else{
     ImgD.width=image.width;
     ImgD.height=image.height;
     }
     }
    }
  }
 
</script>
<title>Anddecor</title>
</head>
<body>
<img alt="" src="pic01.gif" onload= "javascript:DrawImage(this,150,130)" />/*定义图片宽不超过150,高不超过130*/
</body>
</html>

您可能感兴趣的文章:
JS获取图片宽高并根据图片大小自适应
用css让图片自动适应大小
如何用CSS控制图片自适应大小?
jquery中加载图片自适应大小主要实现代码
jquery加载图片自适应大小的代码
40个有创意的jQuery图片和内容滑动及弹出插件收藏集之二
40个有创意的jQuery图片、内容滑动及弹出插件收藏集之一
jQuery实现等比例缩放大图片让大图片自适应页面布局
基于jquery的防止大图片撑破页面的实现代码(立即缩放)
巧用CSS自定义网页下划线样式

[关闭]