CSS DIV Swap without fixed height?
I am curious if it is possible to achieve a simple div swap using only css if the div does not have a fixed height. I have searched far and wide for an answer and have come up with little solid discussion on the matter.
The idea is to have an expansive beer list divided into four separate divs and to display only one of them at a time. When clicking a corresponding anchor, the div displayed would switch while hiding the other three. The first div should display when the page loads. I have a little experience with this and have had success but only with fixed sizes as the foremost div would hide all other divs behind it.
These particular divs will contain formatted tables and unordered-lists. I am not sure if that will have any affect, but thought it was worth mentioning just in case. The divs cannot have a fixed height because the number of items in each will vary. I haven't yet coded it as I am not sure if it is worth the time investment but the HTML would look something like this:
<div id="secondaryNav">
<ul>
<li>Link to div1</li>
<li>Link to div2</li>
<li>Link to div3</li>
<li>Link to di开发者_JS百科v4</li>
</ul>
</div>
<div id="swappedContent">
<div id="1">
<table>
<tr>content</tr> x 40-50
</table>
</div>
<div id="2">
<table>
<tr>content</tr> x 40-50
</table>
</div>
<div id="3">
<table>
<tr>content</tr> x 40-50
</table>
</div>
<div id="4">
<table>
<tr>content</tr> x 40-50
</table>
</div>
</div><!--swappedContent-->
I found an example that I am modeling this layout after (that uses javascript) but I am not sure of the appropriateness of linking to a site which I do not own here. If I should link to it for clarity, please let me know.
Trying using the CSS "display". In each div do:
<div id="myDiv" style="display:none;">
And then when you want to display the div, do the following in JavaScript:
document.getElementById("myDiv").style.display = "block";
So in the end, you'd have something like:
<div id="secondaryNav">
<a href="javascript:void(0)" onclick="showDiv('1')">Link to div1</a>
<a href="javascript:void(0)" onclick="showDiv('2')">Link to div2</a>
<a href="javascript:void(0)" onclick="showDiv('3')">Link to div3</a>
<a href="javascript:void(0)" onclick="showDiv('4')">Link to div4</a>
</div>
<div id="swappedContent">
<div id="1">
<table>
<tr>content</tr> x 40-50
</table>
</div>
<div id="2" style="display:none;">
<table>
<tr>content</tr> x 40-50
</table>
</div>
<div id="3" style="display:none;">
<table>
<tr>content</tr> x 40-50
</table>
</div>
<div id="4" style="display:none;">
<table>
<tr>content</tr> x 40-50
</table>
</div>
</div>
<script type="text/javascript">
function showDiv(divId) {
for (i = 1; i <= 4; i++) {
document.getElementById(i).style.display = "none";
}
document.getElementById(divId).style.display = "block";
}
</script>
Note: be sure to put the script in an appropriate place (usually in the element).
精彩评论