HTML:
<a href="" id="show_link">Show</a><div id="container">
<form class="form">
<a href="" class="align_middle">Link1</a>
</form>
<form class="form">
<a href="" class="align_middle">Link2</a>
</form>
<form class="form">
<a href="" class="align_middle">Link3</a>
</form>
Javascript:
var align_elements = function(item) {
var paddingValue = 20;
$(item).css("position", "static");
$(item).css("float", "left");
$(item).css("width", "auto");
var elementWidth = $(item).width();
var parentElementWidth = $(item).parent().width();
elementWidth = Math.min(elementWidth, parentElementWidth - paddingValue*2);
$(item).css("width", elementWidth);
var elementHeight = $(item).height();
var parentElementHeight = $(item).parent().height();
var topPosition = (parentElementHeight - elementHeight)/2;
var leftPosition = (parentElementWidth - elementWidth)/2;
$(item).css("position", "absolute");
$(item).css("left", leftPosition + "px");
$(item).css("top", topPosition + "px");
$(item).css("float", "");
};
$(document).ready(function(){
var allElements = $(".align_middle");
$.each(allElements, function(index,value){
align_elements(value);
});
$(allElements).bind("DOMNodeInserted",function(){
align_elements(this);
});
$("#show_link").click(function(event){
event.preventDefault();
$("div#container").toggle();
});
});
CSS:
.form {
position: relative;
border-color: brown;
border-width: 3px;
border-style: dotted;
width: 150px;
height: 150px;
float:left;
margin-left: 10px;
overflow: hidden;
}
div#container{
overflow: auto;
margin-top: 10px;
}
I intend to conceal the div#container using display: none; CSS property so that it's not visible on page load, yet this disrupts the functionality of the align_elements feature.
However, when I use a $.get callback function to hide the elements, everything works as expected. (The toggle link also functions properly.)
What is causing this behavior?
Fiddle: http://jsfiddle.net/7PELA/3/