这是一个解决方案。我试图让它看起来像你在你提供的图像中展示它的方式。要展开单击元素,要删除展开,只需再次单击同一元素(我假设这就是您计划设计的方式)。编辑:包括限制一个元素在给定时刻扩展的功能。一探究竟!
http://jsfiddle.net/jccJs/10/
$('#container > div').click(expand);
function global_remove_expand_except(barring) {
$('.exp').each(function (i, cur) {
if (cur !== barring) {
remove_expand($(cur));
}
});
}
function remove_expand($elem) {
$elem.removeClass('exp');
if ($elem.hasClass('swapped')) {
$elem.removeClass('swapped');
swap_back($elem);
}
}
function expand() {
global_remove_expand_except(this);
var $this = $(this);
if ($this.hasClass('exp')) {
remove_expand($this);
} else {
$(this).addClass('exp');
if (!(($.makeArray($this.parent().children()).indexOf(this) + 1) % 3)) {
swap($this);
$this.addClass('swapped');
}
}
}
function swap($elem) {
var after = $elem.next();
$elem.next().remove();
$elem.before($(after).click(expand));
}
function swap_back($elem) {
var before = $elem.prev();
$elem.prev().remove();
$elem.after($(before).click(expand));
}
HTML
<div id="container" class="cf">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
CSS
*{
margin:0;padding:0;
}
/*clearfix credits: http://nicolasgallagher.com/micro-clearfix-hack/*/
.cf:before,
.cf:after {
content: " "; /* 1 */
display: table; /* 2 */
}
.cf:after {
clear: both;
}
#container{
width:350px;
border:3px solid red;
}
#container > div{
float:left;
position:relative;
width:100px;
height:35px;
border:3px solid black;
margin:5px;
text-align:center;
}
#container > div.exp{
background-color:grey;
width:216px;
border-color:transparent;
}
#container > div.exp:after{
position:absolute;
top:20%;
left:18%;
content:"expansion";
}