试试这个jQuery来使页面的一个div中心:
<script>
jQuery.fn.vh_center = function (absolute) {
return this.each(function() {
var t = jQuery(this);
t.css({
position: absolute ? 'absolute' : 'fixed',
left: '50%',
top: '50%',
}).css({
marginLeft: '-' + (t.outerWidth()/2) + 'px',
marginTop: '-' + (t.outerHeight()/2) + 'px'
});
if (absolute) {
t.css({
marginTop: parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(),
marginLeft: parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
});
}
});
};
$(document).ready(function(){
$('#Your_div').vh_center();
});
</script>
如果YOUE使用100%的宽度,如何它有可能使中心的分区?这将占据整个空间....对吗?只有固定宽度才能使其居中。 – geovani075
我的理解是,如果浏览器窗口大于1750px;宽度不再是100%,它是1750px; – user2443235
然后它确定。试试下面的代码 – geovani075