【问题标题】:How to center a div vertically between two div's? jQuery?如何在两个div之间垂直居中div? jQuery?
【发布时间】:2012-08-01 00:25:04
【问题描述】:

我会说我很擅长 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。

【问题讨论】:

  • 它已经工作了吗?如果是,请移至codereview.stackexchange.com,如果不是,请描述错误。
  • 顺便说一句:您可以直接将该函数添加为事件侦听器:$(document).ready(vertical_height); $(window).resize(vertical_height);。此外,没有$(window).ready - 删除它
  • 谢谢,这是我的想法,您可以了解我正在尝试做什么! jsfiddle.net/mattroberts33/nBUnt/1

标签: javascript jquery html center vertical-alignment


【解决方案1】:

这是你的代码,整理成可以运行的东西:

$(function() {
    function vertical_height() {
        var doc_height = $(document).height();
        var head_height = $(".headcontent").height();
        //var mid_height = $(".midcontent").height();//not used
        var foot_height = $(".footer").height();
        var pos_height = head_height + foot_height;
        var neg_height = (head_height - foot_height) / 2;
        var fin_height = doc_height - (pos_height - neg_height);
        $('.midcontent').css({
            "marginTop": "-" + fin_height + "px",
            "marginBottom": "-" + fin_height + "px"
        });
    }
    $(window).resize(vertical_height).trigger('resize');
});

现在您可以专注于让算法正确。

就目前而言,组合和简化表达式,我得到:

var fin_height = doc_height - head_height*3/2 - foot_height*3/2;

这对我来说看起来不对,但我可能是错的。

编辑

为了避免重叠,试试这个版本:

var $$ = {}; //jQuery object cache.
$$.w = $(window);
$$.h = $(".headcontent");
$$.m = $(".midcontent");
$$.f = $(".footer");
function vertical_height() {
    var w = $$.w.height(),
        h = $$.h.outerHeight(true),
        m = $$.m.outerHeight(),
        f = $$.f.outerHeight(true),
        fin_height = Math.max(0, (w - h - m - f) / 2);
    $$.m.css('marginTop', Math.floor(fin_height)).css('marginBottom', Math.ceil(fin_height));
    $$.h.text(fin_height);
};
$(window).on('resize', vertical_height).trigger('resize');

DEMO

注意事项

  • position:absolute 从页脚的 CSS 中移除
  • jQuery 对象现在缓存在 $$ 中,以减少调整大小处理程序的工作量。
  • 现在基于窗口高度而非文档高度进行计算。
  • 现在使用.outerHeight(false) 测量三个 div,包括垂直填充和边框。
  • Math.max(0, ...) 可防止重叠,从而确保鳍片高度不会为负值。
  • $$.h.text(fin_height); 语句用于调试,可以删除。

编辑 2

以下代码将“消除”调整大小事件。

替换:

$(window).on('resize', vertical_height).trigger('resize');

与:

var do_resize;
$(window).on('resize', function(){
  clearTimeout(do_resize);
  do_resize = setTimeout(vertical_height, 100);
}).trigger('resize');

【讨论】:

  • 理论上,1)我将页眉和页脚从文档高度中取出,2)然后将页眉和页脚之间的差异除以二,3)最后我正在从页眉和页脚高度中取出我在数字“2”中得到的东西......
  • 我不是 100% 确定该怎么做,但目标是有两个 div 底部的一个是绝对定位的,我希望中间 div 垂直居中。我之前已经想通了,但是它使用了很多层的 z-index,我想这样做,这样如果我要缩小页面,div 就不会重叠 - 现在它只是底部 div重叠,但那是因为它是绝对位置。
  • jsfiddle.net/nBUnt/14 我已经走到这一步了,哈哈,现在我该怎么做才能使中间的 div 不与顶部的 div 重叠,但在没有足够的时候保持在它的正下方居中的空间?
  • jsfiddle.net/nBUnt/14 顶部和中间的空间比中间和底部的空间要小很多,而且似乎随着它每移动一个像素,它就在上升时有某种意义上的振动或向下...虽然我们越来越近了,但空间需要相同,我们将如何做到这一点?
  • 你,我的朋友,太棒了!我在任何地方都没有看到过这种情况,这似乎是人们会考虑但不会尝试去做的事情......希望这对其他人也有帮助!
【解决方案2】:

计算时只是一个小提示,在得到最终数字之前不要四舍五入。

h = Header Height

f = Footer Height

m = Middle Height

d = Document Height

s = Height of the space above or below the div

这里我们假设文档的高度等于所有其他元素组合的高度。由于中间 div 的上方和下方有一个相等的空格,因此我们有两个空格。

d = h + f + m + 2s

d - h - m -f = 2s

(d - h - m - f) / 2 = s

让我们把它放到一些javascript中

$(document).ready(function() {

    $(window).resize(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 fin_height = Math.round((doc_height - head_height - mid_height - foot_height) / 2);
    $('.midcontent').css("margin-top", fin_height)
  }
});

对于 css,我们只需要添加一个上边距,因为这会将其推离页眉,因为我假设绝对定位的页脚将卡在底部。

这是一个有效的小提琴:http://jsfiddle.net/nBUnt/14/

【讨论】:

  • 你对我在做什么是完全正确的!我只是不明白如何让它工作......在这里:jsfiddle.net/mattroberts33/nBUnt/4
  • 啊!我用了你的方程,所以它有效!好吧,在大多数情况下...jsfiddle.net/nBUnt/14我已经走了这么远,哈哈,现在我该怎么做才能使中间 div 不与顶部重叠,但在没有时保持在它的正下方有足够的空间居中吗?
  • 如果您添加检查以确保 fin_height 不是负数,它应该可以工作。我会更新。 jsfiddle.net/nBUnt/19 您必须解决的另一个问题是页脚,因为它是绝对定位的,所以它会超出中间内容和头部内容。我建议实施一个粘页脚技术,例如ryanfait.com/sticky-footer
  • 我喜欢粘性页脚的想法,我会试试看。
猜你喜欢
  • 2019-12-30
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
  • 2017-07-29
  • 1970-01-01
  • 2013-03-11
  • 2010-10-26
  • 1970-01-01
相关资源
最近更新 更多