你可以得到图像尺寸,然后强制在一个固定的高度/宽度DIV他们定位为背景图像。图像加载后,您可以通过javascript获取宽度的高度。
IMG onload事件看:http://www.w3schools.com/jsref/event_img_onload.asp
这里就是你要做的
<img onload="redoImage(this)" src="<%=user["pic_big"]%>" id="pic1">
或通过JS
var img=new Image();
img.onload = redoImage();
img.src="<%=user["pic_big"]%>";
那么这才是真正的地方,图片显示了和你”用适当的偏移量设置它的背景图像以使图像正确显示。花点时间计算出如何获得偏移x或偏移y。
<div id="pic1-holder" style="width:120px; height:120px;"> </div>
function redoImage(this) {
var xOffset = computeXoffset(this.width, 120);
var yOffset = computeYoffset(this.height, 120);
// now set the background image of the div
// and the offset of that background image
};
我的生产代码,奇妙做到这一点。快乐的编码。
非常棒!谢谢! – MonsieurNinja 2012-03-07 09:56:01