显示数据时,我使用此代码将图像居中在结果 div 内的缩略图字段中:
<div class='result'>
<div class='date_field'>15 Okt</div>
<div class='thumbnail_field th_div'><img src='p.png' class='thumb'></div>
<div class='content_field'><a href="p.html">p</a></div>
<div class='price_field'>5</div>
</div>
.result{position: relative;background-color: white;height: 100px;overflow: hidden;margin: 0px 10px;}
.thumbnail_field{position: absolute;top: 8;left: 15%;width: 100px;height: 75px;background-color: #FFF;}
.th_div img{position: absolute;}
$(document).ready(function(){
var h = 75;
var w = 100;
$('.thumb').each(function(){
$(this).load(function() {
var width=$(this).width();
var height=$(this).height();
var ratio=width/height;
var pwidth=$(this).parent().width();
var pheight=$(this).parent().height();
var newheight;
var topmargin;
var newwidth;
var leftmargin;
if(width/height>=4/3){
$(this).css("width",w);
newheight=w/ratio;
topmargin=(h-newheight)/2;
$(this).css("top",topmargin+"px")
}else{
$(this).css("height",h);
newwidth=h*ratio;
leftmargin=(w-newwidth)/2;
$(this).css("left",leftmargin+"px")
}
});
});
});
它在 Chrome 和 Firefox 中运行,但在 IE 8 中随机运行。关于可能是什么原因的任何提示?