简体   繁体   English

高度自动div扩展高度

[英]Height auto div to expand height

Please check: http://wixwebsite.seobrasov.com for reference. 请检查: http : //wixwebsite.seobrasov.com以供参考。 My goal here is to achieve a body/wrapper div height according to the content instead of having a scrollbar for a 3500px height body on a 500px content. 我的目标是根据内容实现body / wrapper div的高度,而不是在500px内容上具有3500px高度的滚动条。

I have a one page design with divs sliding in and out. 我有一页设计,div可以滑入和滑出。 There is a wrapper with overflow hidden and position relative that contains all the divs. 有一个隐藏了溢出且位置相对的包装器,其中包含所有div。 Inside that, there are the divs having position absolute and height auto. 在里面,有div具有绝对位置和高度自动位置。 Inside each div there are the content divs with height aut as well and they correctly expand to fit their content. 在每个div内,也有具有高度自定义的内容div,它们会正确扩展以适合其内容。 It is all connected to a javascript that does the sliding. 它们都连接到执行滑动操作的javascript。 The whole thing only works if I set a fixed height to the wrapper div. 仅当我将包装器div设置为固定高度时,整个过程才起作用。 Otherwise, having height auto on the wrapper or using javascript to set the wrapper div to the inner div height (which is height auto as well) makes the page not to expand & show any content AT ALL. 否则,在包装器上设置高度为自动,或使用JavaScript将包装器div设置为内部div的高度(也为height),会使页面无法展开并完全显示任何内容。

The first thing you would think about would be that the wrapper div does not expand height due to position absolute of the inner divs. 您首先要考虑的是,由于内部div的位置绝对,包装div不会扩展高度。 That is only part of the problem. 那只是问题的一部分。 If I do indeed change the position to relative, it will only show part of the divs. 如果我确实将位置更改为相对,则只会显示部分div。 I have tried using javascript to set the wrapper div to take position from inner divs, but those inner divs also have height auto. 我试过使用javascript设置包装div从内部div取得位置,但是这些内部div也具有height auto。 And I cannot do the javascript on the content divs as there are more using the same class and having different heights, as they expand depending on content. 而且我无法在内容div上执行javascript,因为有更多使用相同类且具有不同高度的javascript,因为它们会根据内容进行扩展。

So the question that follows is: Even if I achieve the wrapper div to expand height to its containing divs, wouldn't that height be the height of the biggest div? 因此,接下来的问题是:即使我实现了包装div来将height扩展到其包含的div,那个高度不是最大div的高度吗? Since they are all on the same page? 既然它们都在同一页面上?

Here is some code: 这是一些代码:

<div class="content-wrap">

   <div class="dhome">
      content
    </div>

    <div class="dabout">
       content
    </div>

    etc.

.content-wrap{
overflow:hidden;
position:relative;
clear:both;
height: 3500px -> aiming for auto
}

.dhome,.dabout{
position:absolute;
right:-200%;
height:auto;
}

So far the only solution I'm seeing to this would be to place the content on different pages but I don't think that I'll manage to do the sliding. 到目前为止,我所看到的唯一解决方案是将内容放置在不同的页面上,但是我认为我不会设法进行滑动。

Thanks in advance, 提前致谢,

So I got this Javascript that does the animation: 所以我得到了执行动画的Javascript:

function animate() {
var currentPageI = -1;
var pages = [
    $('div.dhome'),
$('div.dabout'),
];
var viewsWidth = 1300; 
var showPage = function(index){
    if(index === currentPageI){return;}
    var currentPage = pages[currentPageI];
    if(currentPage){
        currentPage.stop().animate({left: -viewsWidth})
    }
    var nextPage = pages[index];
    nextPage
        .stop()
        .css({left: viewsWidth + Math.max(0,(($(window).width() - 980)/2))})
        .animate({left: Math.max(0,(($(window).width() - 980)/2))})
        currentPageI = index;
}; 
showPage(-1);
$('a.dhome').click(showPage.bind(null, 0));
$('a.dabout').click(showPage.bind(null, 1));
$(document).ready(function () {
animate();
});

First of all I have added the suggested Javascript at the end of this one and didn't do anything...after that I have added it into the animation script and used nextPage instead of the wrapper childNodes, and it still didn't do the trick. 首先,我在该代码的末尾添加了建议的Javascript,但没有做任何事情...之后,我将其添加到了动画脚本中,并使用nextPage而不是包装childNodes,但它仍然没有做诀窍。 I will further look into this. 我将进一步调查。

Thank you! 谢谢!

set an ID on the div with class="content-wrap" 使用class =“ content-wrap”在div上设置一个ID

var wrapper=document.getElementById(IDcontentwrap);
var childNode, childNodes=wrapper.childNodes, i, l=childNodes.length;
var maxWidth=0, maxHeight=0;
for (i=0;i<l;i++)
    {
    childNode=childNodes[i];
    if (childNode.nodeType==1) 
        {
        if (maxWidth<childNode.offsetWidth) maxWidth=childNode.offsetWidth;
        if (maxHeight<childNode.offsetHeight) maxHeight=childNode.offsetHeight;
        }
    }
wrapper.style.width=maxWidth+"px";
wrapper.style.height=maxHeight+"px";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM