baigao
2015-09-25 23:04:16
证
jquery实现将html div在窗口中水平垂直居中效果,无论是否调整窗口大小
jquery实现将html div在窗口中水平垂直居中效果,无论是否调整窗口大小
如图所示:
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>代码实现了字div在父div中的水平垂直居中效果</title>
<style type="text/css">
.parent
{
width:500px;
height:500px;
background-color:red;
}
.children
{
width:200px;
height:100px;
background-color:green;
}
</style>
<script type="text/javascript" src="http://static.zuidaima.com/resource/js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(window).resize(function(){
$(".children").css({
position:'absolute',
left:($(".parent").width() - $(".children").outerWidth())/2,
top:($(".parent").height() - $(".children").outerHeight())/2 + $(document).scrollTop()
});
});
$(window).resize();
})
</script>
</head>
<body>
<div class="parent">
<div class="children"></div>
</div>
</body>
</html>
猜你喜欢
请下载代码后再发表评论
相关代码
最近下载
最近浏览
jenkens LV3
9月23日
511076827 LV33
2024年3月31日
9323249323 LV13
2023年7月25日
微信网友_6411724627349504 LV3
2023年4月3日
nininininini LV2
2022年5月7日
muimuimui LV9
2021年2月13日
熊憨憨2号 LV1
2020年11月18日
NANA123 LV5
2020年6月22日
yale1422688399 LV3
2020年5月29日
东风风神发顺丰 LV2
2020年2月22日





