我会说我很擅长 HTML 和 CSS,当我开始接触 JavaScript 时,我会感到很挣扎;我可以理解其中一些,但我无法为我的生活写下它!我正在努力学习,无论如何;我将这个 jQuery 脚本放在一起,目标是在相对定位的顶部 div 和绝对定位的底部 div 之间垂直居中 div。不适合我,哈哈。
因为我正在尝试学习如何使用 jQuery 并创建自己的脚本,所以我想尝试让它工作。但是,如果有更好的方法来实现我的目标,我也将非常感谢您以这种方式提供的意见!
<script type="text/javascript">
$(document).ready(function() {
$(window).ready(function(){
vertical_height()
});
$(window).resize(function(){
vertical_height()
});
function vertical_height(){
var doc_height = $(document).height();
var head_height = $(".headcontent").height();
var mid_height = $(".midcontent").height();
var foot_height = $(".footer").height();
var pos_height = Math.round(head_height+foot_height);
var neg_height = Math.round((head_height-foot_height)/2);
var fin_height = Math.round(doc_height-(pos_height-neg_height));
$('.midcontent').css({
"marginTop","-"+fin_height+"px",
"marginBottom","-"+fin_height+"px"
}
}
});
</script>
.headcontent是顶部的 div。
.midcontent是我想要居中的中间 div。
.footer是底部的 div。