尝试使用 javascript 存储每个图像,width
然后height
将以下样式应用于循环中的图像:
position: absolute;
top: 50%;
margin-top: -1*($height/2); /* $height = stored height of current image */
right: 50%;
margin-right: -1*($width/2); /* $width = stored width of current image */
申请li
时position: relative;
jQuery代码:
$(function() {
var imgs = $('ul > li img');
$.each(imgs, function(i, img) {
/* cache variable for better performance */
var $img = $(img);
$img.css({
'margin-top': $img.height()/-2,
'margin-left': $img.width()/-2
});
});
});
CSS:
li {
float: left;
height: 100px;
width: 100px;
overflow: hidden;
margin: 10px;
padding: 10px;
position: relative;
}
li:hover { overflow: visible; }
li img {
position: absolute;
display: block;
top: 50%;
left: 50%;
}