js 实现网页图片缩略图按比例缩放

<%@ Page Language="C#" CodeFile="ImageAutoZoom.aspx.cs" Inherits="UI_ImageAutoZoom" %>

<!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 runat="server">

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 <title>js 实现网页图片按比例缩放-by wangtao</title>

 <script type="text/javascript" src="../Misc/Js/Core/Jquery.js"></script>

</head>

<body>

<script type="text/javascript">

function ImageAutoZoom(Img,FitWidth,FitHeight)

{

var image=new Image();

image.src=Img.src;

if(image.width>0 && image.height>0)

{

 if(image.width/image.height>= FitWidth/FitHeight)

 {

 if(image.width>FitWidth)

 {

  Img.width=FitWidth;

  Img.height=(image.height*FitWidth)/image.width;

 }

 else if(image.hight>FitHeight)

 {

  Img.height=FitHeight;

  Img.width=(image.width*FitHeight)/image.height;

 }

 else

 {

  Img.width=FitWidth;

  Img.height=FitHeight;

 }

 }

}

}

</script>

 <form id="ImageAutoZoomform" runat="server">

 <div>

 <img src="../Misc/Image/Test/war3.jpg" height="200" width="200" alt="按比例缩放图片" onload="ImageAutoZoom(this,200,200)" />

 </div>

 </form>

</body>

</html>

因为当图片太大的时候,页面会造成一种拉动,所以最好设一下height,width这样可以减少拉动的范围。
虽然这个没有很大的技术含量,但是很实用,大部分网站用到图片的地方,多多少少都会涉及到的。
posted on 2012-03-10 15:26  cy凌云  阅读(1142)  评论(0)    收藏  举报