您可以从 javascript 获取图像尺寸,然后将它们强制定位为固定高度/宽度 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
};
我有生产代码可以很好地做到这一点。快乐编码。