简体   繁体   中英

Div height auto issue

I have a #val container div that stretches to accommodate Javascript content within a div called #cat-container.

cat-container has 4 tabs above it and are numbered 1 - 4, so if you click 1, it will load content from a php array, and similar for tabs 2 -3.

When you click on each tab, #val-container successfully stretches to the correct height, however I have set tab 1 and its content to display by default as the page loads. The problem is that the content from all the other tabs gets loaded into tab 1 onpage load.

<script type="text/javascript">
var i=1;
var tab;
document.getElementById('cat-container').style.position='relative';
document.getElementById('val-container').style.height='auto';
while(tab=document.getElementById('option'+i+'-body'))
{
    tab.style.position='absolute';
    tab.style.top='0';
    tab.style.left='0';
    i++;
}
var urllocation = location.href; //find url parameter
if(urllocation.indexOf("#") == -1)
{
    displayTab(1);
}
else
{
    if(urllocation.indexOf("#option1")>-1)displayTab(1);
    else if(urllocation.indexOf("#option2")>-1)displayTab(2);
    else if(urllocation.indexOf("#option3")>-1)displayTab(3);
    else if(urllocation.indexOf("#option4")>-1)displayTab(4);
    else displayTab(1);
}
</script>

Essentially I would like tab 1 to just show just its content on page load.

This line is the issue:

document.getElementById('val-container').style.height='auto';

So if the dispalyTab(1), just show its content!

The code that actually displays the tabs:

 <script type="text/javascript">
function displayTab(num)
{
    var tab,x;
    x=1;
    while(tab=document.getElementById('option'+x+'-body'))
    {
        if(num!=x)tab.style.display='none';
        else tab.style.display='inherit';
        x++;
    }
}
 </script>

 <div class="category-tab" id="aoption1"><a href="#option1" onclick="displayTab(1);return false;">Tab1</a></div>
<div class="category-tab" id="aoption2"><a href="#option2" onclick="displayTab(2);return false;">Tab2</a></div>
<div class="category-tab" id="aoption3"><a href="#option3" onclick="displayTab(3);return false;">Tab3</a></div>
<div class="category-tab" id="aoption4"><a href="#option4" onclick="displayTab(4);return false;">Tab4</a></div>
<br><br>

Here is the PHP/HTML:

I have used tab 1 - 4 to simplify things but in actual fact they represent price ranges:

  echo "<div id=\"cat-container\">";
echo '<div id=\"val-contain\">';

 $cats=array(0,1000,5000,10000,100000);
for($ci=1;$ci<count($cats);$ci++)
{
    if($ci==4)echo "<div class=\"category-body\" id=\"option".$ci."-body\"><a   name=\"option".$ci."\"><h3>Gifts for over &pound;100!</h3></a>";
    else echo "<div class=\"category-body\" id=\"option".$ci."-body\"><a name=\"option".$ci."\"><h3>Gifts for under ".fixedToFloatCurrency($cats[$ci])."!</h3></a>";
    $i=0;
    for ($p = 0; $p < count($game); $p++ )
    {
        $game[$p]->getProduct();
        if($game[$p]->price<$cats[$ci] && $game[$p]->price>=$cats[$ci-1])
        {
            if (($i % 3) == 0 )
            {
                if($i)echo "</tr></table>";
                echo "<table id=\"newarrivals\" style=\"padding-top:5px;\"><tr>";
            }

            echo "<td>";
            $game[$p]->getLink();
            echo $game[$p]->link;
            echo "<h2 class=\"section-tab-game2\">".$game[$p]->name."</h2>";
            echo "<div class=\"container\" style=\"text-align:center;\"><div      class=\"image-spacer\">";
            echo $game[$p]->getImage();
            echo $game[$p]->image;
            echo "</div>";
            echo "<div class=\"specialprice\" >";

            if(!is_numeric($game[$p]->price)){
                echo $game[$p]->price;
            }
            else
            {
                if($game[$p]->price < $game[$p]->maxprice)
                {echo "From only: &pound;".fixedtofloat($game[$p]->price);}
                else
                {echo "Only: &pound;".fixedtofloat($game[$p]->price);}
            }
            echo "</div></div></a>";
            echo "</td>";
            $i++;
        }
    }
    echo "</tr></table></div>";
}
echo "</div>";
echo '</div>';
  ?>

CSS:

#val-contain { }
#cat-container {  }
.category-tab { width:125px;border:2px solid white; height: 50px; background:url('images/design-  gac/valentines-navigation.png')0 0 no-repeat;float:left; text-align:center; font-family:Arial,sans-serif;font-size:12pt;font-weight:bold; color:white; padding-top: 7px;}
.category-tab a { color:white; }
.category-tab a:hover { color:#white; }
.category-body { width:515px; border:1px solid #3c2130; height: auto; overflow:hidden; background:url('images/design-gac/valentines-navigation-back.png')0 0 repeat-x;}
.category-body h3 { font-size:27pt; text-align:center; color: #ffffff; font-family: Arial,sans-    serif;}
.container { background-color:white; }
.imgpad { margin-bottom: 10px; }
.vallinks {color: #c50f07; font-weight: bold; }

Hope someone can help?

Volterony

This is a pure html/inline css solution with one static tab.

<body>
    <style>
        html{
            height: 100%;
        }
        body {
            min-height: 100%;
        }
    </style>

    <div style="max-height: 5%;min-height: 5%; background-color: #d9edf7;display: block">

        <div class="category-tab" style="float: left" id="aoption1"><a href="#option1" onclick="displayTab(1);
            return      false;">Tab1</a></div>
        <div class="category-tab" style="float: left" id="aoption2"><a href="#option2" onclick="displayTab(2);
            return false;">Tab2</a></div>
        <div class="category-tab" style="float: left" id="aoption3"><a href="#option3" onclick="displayTab(3);
            return false;">Tab3</a></div>
        <div class="category-tab" style="float: left" id="aoption4"><a href="#option4" onclick="displayTab(4);
            return false;">Tab4</a></div>    
    </div>


    <div style="max-height: 95%;min-height: 95%; background-color: red;display: block">
        Tab 1 content
    </div>

</body>

you used 'val-contain' as your div's id

echo '<div id=\"val-contain\">';

but you are trying to change it with

document.getElementById('val-container').style.height='auto';

change the div's id to val-container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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