I have been trying to learn a little bit about jQuery's .animate()
function, and I have gotten a few things to animate, but I haven't been able to set up animation for my table in the way that I would like.
Here's the table html:
<div class="topSectionContainer">
<div id="dropDownArrow">►</div><span class="editLabelTitle">Page Settings</span>
<table class="topSectionTable">
<tr>
<td class="pageSettingsContainer"></td>
<td class="fileBoxContainer">@Html.Raw(ContentGenerator.getFileBox("Tourism"))</td>
</tr>
</table>
</div>
I would like to get the following functionality:
- The
table.topSectionTable
starts off as though it haddisplay: none
assigned. - When
div#dropDownArrow
is clicked it should (while animating) appear that the table is growing in height (whether the height property is actually adjusted or not) and revealing the contents of the table as it expands. - Once
div#dropDownArrow
is clicked again, it should animate in reverse, thus hiding the table and shrinking its height (or appearance thereof).
I have already used some simple code for this that does not have animation (jQuery):
$("#dropDownArrow").toggle(function () {
$(".topSectionTable").css("display", "table");
$("#dropDownArrow").html("▼");
},
function () {
$(".topSectionTable").css("display", "none");
$("#dropDownArrow").html("►");
});
Things I have tried:
- Using jQuery's
.animate()
with the display property. I am not sure of the reason for failure here, as the actual change in the display property doesn't show, but I'm guessing that changes to thedisplay
property are not supported with jQuery's.animate()
. - I have also tried setting the CSS rules for
table.topSectionTable
to reflect bothoverflow: hidden;
andheight: 0px;
, then animating only the height property. Here, the animation on height was successful, however, the contents oftd.fileBoxContainer
show up whether the height is 0 or not (even though the height expands and contracts on the clickings of thediv#dropDownArrow
element.
I've seen this done all the time on websites, so I know there is a way. Furthermore, I would like to do this in jQuery rather than just CSS3 because I would like to retain this functionality in IE8, as well, if possible, and I know CSS3 has no chance of doing this.
UPDATE -- TRYING WITH HEIGHT 0 AND OVERFLOW HIDDEN, PLUS JQUERY ANIMATE
jQuery Code:
$("#dropDownArrow").toggle(function () {
$(".topSectionTable").animate({
height: 100}, 1000);
$("#dropDownArrow").html("▼");
},
function () {
$(".topSectionTable").animate({
height: 0}, 1000);
$("#dropDownArrow").html("►");
});
CSS:
table.topSectionTable
{
height: 0;
overflow: hidden;
}
td.pageSettingsContainer
{
}
td.fileBoxContainer
{
}
And the HTML is the same as above
My C# getFileBox Method:
public static string getFileBox (string location)
{
string content = "";
string[] files = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/CMS Files/" + location + "/"));
foreach (var file in files)
{
content += Path.GetFileName(file);
content += "<br/>";
}
return content;
}
` elements. Even still, it doesn't start hidden (nor does it ever get hidden). – VoidKing Sep 30 '13 at 16:26
Tourism_MS_Building.jpg