2013-05-15 111 views
1

假设我有一个“容器”元素,其中包含overflow:auto,其中包含“contents”div。容器的高度为窗户高度的百分比,内容可以是任何高度的要求如何在页面加载时可靠地获得两个元素的尺寸

<div id="container">// Height dependent on window size 
    <div id="content"> 
    // Arbitrary amount of content here 
    </div> 
</div> 

CSS - 请注意我在“现实世界”的场景,内容元素实际上是一个表,与规则如下图所示:

#container { 
    height:100%; 
    overflow:auto 
} 

#content { 
    border-collapse: collapse; 
    border-spacing: 0; 
} 

我想要做的是检查内容的身高是否超过容器的,如果真正做一些事情。

但是,如果我简单地做:

$(document).ready(function(){ 
    var containerH = $('#container').outerHeight(); 
    var contentH = $('#content').outerHeight(); 
    if (contentH > containerH) { 
     // If the content's too big, do things... 
    }; 
}); 

if语句永远不会运行,因为这两个高度是为等于总计算 - 即使我知道这是情况并非如此。随后的检查总是显示实际尺寸。所以我想:也许元素没有完成渲染(我使用@ font-face来设置元素中的文本样式 - 也许我需要等待完成),然后我尝试去做类似于:

$(document).ready(function(){ 

    var container = $('#container'); 
    var content = $('#content'); 

    function getProperHeight(el){ 
     var height = el.load(function(){ 
      var h = this.outerHeight(); 
      return h; 
     }); 
     return height; 
    }; 

    var containerH = getProperHeight(container); 
    var contentH = getProperHeight(content); 

    console.log(containerH + ' ' + contentH) 

}; 

这是我使用的方法load()最接近的,但我得到的翻译:两个预期值。

总之 - 我怎样才能得到两个元素的实际渲染尺寸?

+0

我觉得你的问题是,你正在做的这$计算(文件)。就绪()。您应该尝试将此代码放置在按钮单击事件上并查看它是否有效。 – Hoffmann

+0

我看到你建议的逻辑,但这是在页面加载时工作所必需的。获取用户事件的高度是微不足道的,因为这些元素几乎肯定会被加载。 – verism

+0

我只是想让你试着看看它是否可以在点击按钮上工作。如果它在按钮上工作,那么问题是在$(document).ready()被调用的时候,DOM并没有实际处理你的CSS来按照你想要的方式进行计算。如果是这种情况,您可以在JavaScript上设置CSS或探索其他事件的可能性。 – Hoffmann

回答

-1

,你可以像这样...

HTML

<div id="container"> 
    <div id="listingNav"> 
    <a id="back">Back </a> 
    <div id="paginationNav"></div> 
    <a id="next"> Next</a> 
    </div> 
    <div id="content"> 
    // Arbitrary amount of content here 
    </div> 
</div> 

CSS

#content { 
    overflow:hidden; 
} 

的Javascript

//set how tall you want you container to be 
var divHeight = 624; 

$(document).ready(function() 
{ 
    //get the height of the container without styles 
    var contentHeight = $('#content').height(); 
    //then add styles to the container 
    $('#content').css('height', divHeight + 'px'); 
    //find out how many pages there will be 
    var divPages = Math.ceil(contentHeight/divHeight); 
    //loop through and create the page # anchors 
    for(var i = 1; i <= divPages; i++) 
    { 
     $("#paginationNav").append('<a id="' + i + '" onclick="javascript:goToPage(this)">' + i + '</a>'); 
    } 
    //wrap every in #content with a div with the class of .content 
    $('#content').children().wrapAll('<div class="content"/>'); 
    //program the paganation effect for next 
    $('#next').click(function() { 
     var current = ($('.content').css('margin-top') || '0px'); 
     if((current.substring(0,current.length-2) - divHeight) > -contentHeight) 
     { 
      $('.content').css('margin-top',current.substring(0,current.length-2) - $('#listingContainer').height() + 'px'); 
     } 
    }); 
    //program the paganation effect for back 
    $('#back').click(function() { 
     var current = ($('.content').css('margin-top') || '0px'); 
     if(current.substring(0,current.length-2) < 0) 
     { 
      $('.content').css('margin-top',(current.substring(0,current.length-2) - -$('#listingContainer').height()) + 'px'); 
     } 
    }); 
}); 
//program the paganation effect for each of the page # anchors 
function goToPage(page) 
{ 
    $('.content').css('margin-top', -((divHeight * page.id) - divHeight)); 
} 

希望这有助于。

哦.....这是使用jQuery 1.9.1

+0

谢谢你的深入探讨 - 但我不确定这与我的问题有关。我想衡量的高度,而不是像你的榜样那样设置它们。另外,我不确定你为什么包含分页技术 - 这很有趣,但我并没有真正要求这个问题。 – verism

+0

好吧,你说过你想比较两个div的高度,然后根据比较做一些事情。分页的东西只是想到的第一件事。 –

+0

从我所看到的##容器中使用的样式使其与页面一样大,或者将它放在另一个尽可能大的位置,这意味着'#content'和'#container'将始终高度相同。为了做一个比较,你需要将'#容器'的高度设置为更确定的东西,无论是px还是%。 –

相关问题