这是我的代码
<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 完全居中,请帮助我,我快疯了,我试图改变
这个
<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>
有了这个(TXH TO Elhussein Hashem)
<script>
$(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
});
}
</script>
但结果没有改变:(
我注意到,即使我刷新页面,当我缩小并重新打开浏览器窗口时,一切都会回到中心
这是DIV
<div class="className">
<div><img style="width:100%" src="http://i39.tinypic.com/5f506d.jpg"/></div>
</div>
帮助我