这是我的代码
<style>
.className{
width:55%;
height:auto;
margin:0 auto;
}
body{
text-align:center;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(window).resize(function(){
$('.className').css({
position:'absolute',
left: ($(window).width() - $('.className').outerWidth())/2,
top: ($(window).height() - $('.className').outerHeight())/2
});
});
// To initially run the function:
$(window).resize();
});
</script>
当我打开我的页面时,我看到我的DIV没有居中,但是当我减少并且我在我的浏览器的窗口中重新打开时,DIV完全居中,PLZ帮助我,我疯了
答案 0 :(得分:0)
试试这个,它应该有效:
$(document).ready(function(){
resizeFunction();
window.onresize = function() {
resizeFunction();
};
resizeFunction();
});
function resizeFunction(){
$('.className').css({
position:'absolute',
left: ($(document).width() - $('.className').outerWidth())/2,
top: ($(document).height() - $('.className').outerHeight())/2
});
}