开发者

dynamically positioning panels depending on their user-controlled visibility

I've got 4 panels, and I have a checklist at the top which controls which panels you want to view. How would I set it up so that if they uncheck one of the panels, then the rest will reposition. A bit vague, so:

    ___   ___
   | 1 | | 2 |
   |___| |___|
    ___   ___
   | 3 | | 4 |
   |___| |___|

So if they uncheck 2 and 3, I would like 4 to be position next to 1

    ___   ___
   | 1 | | 4 |
   |___| |___|

And if they unchecked just 3, then 4 would go to the 3's position. These panels would also be centered on the page, but if the something like the 3 is unchecked, I want the fourth panel to be left aligned with the first panel, and not centered between them. So like this:

    ___   ___
   | 1 | | 2 |
   |___| |___|
    ___
   | 4 |
   |___|

And not this:

 开发者_如何学运维   ___   ___
   | 1 | | 2 |
   |___| |___|
       ___
      | 4 |
      |___|


Placing the 4 panels (same fixed size) within a fixed container which is at least twice the width of each panel but less than 3 times. Float the panel. Example as follows:

CSS:

#panels {
    width: 400px;
}
.panel {
    float: left;
    width: 190px;
    height: 100px;
    border: solid 1px #f00;

}

JS:

$(function(){
    $("div input[type='checkbox']").attr('checked',true);
    $("div input[type='checkbox']").change(function() {
            if($(this).attr('checked'))
                $('#' + $(this).val()).show();
            else
                $('#' + $(this).val()).hide();
        }
    );
});

HTML:

<div>
<label for="p1"><input type="checkbox" value="panel1" />1</label>
<label for="p2"><input type="checkbox" value="panel2" />2</label>
<label for="p3"><input type="checkbox" value="panel3" />3</label>
<label for="p4"><input type="checkbox" value="panel4" />4</label>
</div>
<div id="panels">
<div class="panel" id="panel1">1</div>
<div class="panel" id="panel2">2</div>
<div class="panel" id="panel3">3</div>
<div class="panel" id="panel4">4</div>
</div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜