关于IE6中产品图片过大导致的错位处理办法
以前做企业网站老是遇到客户上传的产品图片过大(指图片的长和宽),从而导致在IE6中网页严重错位。如图:
但是又不能用 style="overflow:hidden;",这个虽然解决了错位,但是图片被剪掉了一块,看起来别扭:如图:
正常的应该是这样:
解决办法:
在产品展示页面中用JQurey控制下:
/*这个标签调用产品图片*/
<asp:Image runat="server" ID="ProductBigImg" />
首先引入jquery
<script src="/Utility/js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#<%=ProductBigImg.ClientID%>").each(function () {
var maxWidth = 680; // 图片最大宽度,这个数字视网页给产品图片展示的具体宽度而定。(其实把这个方法封装起来,
宽度作为一个参数传递会更好,懒,没去做:))
var ratio = 0; // 缩放比例
var width = $(this).width(); // 图片实际宽度
var height = $(this).height(); // 图片实际高度
// 检查图片是否超宽
if (width > maxWidth) {
ratio = maxWidth / width; // 计算缩放比例
$(this).css("width", maxWidth + "px"); // 设定实际显示宽度
height = height * ratio; // 计算等比例缩放后的高度
$(this).css("height", height + "px"); // 设定等比例缩放后的高度
}
});
});
</script>
通过这样处理下,图片就会等比例缩小,在IE6中也不会错位了。
最近又找到一种更好的方法,兼容性也很好。代码如下,可以找个图片试试:
<!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">
<title>css等比例缩小图片</title>
<head>
<style type="text/css">
<!--
body {
text-align: center;
margin: 20px auto;
padding: 0px;
font-size:14px;
color:red
}
#pic{
margin:5px auto;
width:500px;
padding:0;
border:1px solid #333;
}
#pic img{
max-width:200px;
width:expression(document.body.clientWidth > 200?"200px":"auto" );
display:block;
border:0
}
-->
</style>
</head>
<body>将宽度强制为200px后图片高度被等比例缩小
<div id="pic">
<a href="#"><img src="wall5.jpg" /></a>
</div>
原图:
<br />
<img src="wall5.jpg" />
</body>
效果图如下: