2013-02-21 56 views
0

我有一个包含两列的主列表。在这些列中,每个div都有多个类,当您选择其中一个类时,我想要排列这些列以使它们均匀。这里是我开始用的一个例子:jQuery对两列内的div进行排序

<div class="left"> 
    <div class="dot blue">blue one</div> 
    <div class="dot red">red one</div> 
    <div class="dot orange">orange one</div> 
    <div class="dot red">red two</div> 
    <div class="dot red">red three</div> 
</div> 
<div class="right"> 
    <div class="dot red">red four</div> 
    <div class="dot blue">blue two</div> 
    <div class="dot blue">blue three</div> 
    <div class="dot blue">blue four</div> 
    <div class="dot orange">orange two</div> 
</div> 

点击拨动按钮,我对“红”之后,我想隐藏一切,除了红点,但两者之间的红色的均匀排序列。我可以正确隐藏所有内容,但不知道如何在“左”和“右”div之间制作四个红色div。这里是我想要的输出:

<div class="left"> 
    <div class="dot red">red one</div> 
    <div class="dot red">red two</div> 
</div> 
<div class="right"> 
    <div class="dot red">red three</div> 
    <div class="dot red">red four</div> 
</div> 

在此先感谢。

+0

纳米小姐读... – defaultNINJA 2013-02-21 16:00:25

回答

0

将所有.red s到左侧,然后找到中间点和组下半年移回右:

$('.left,.right').find('.dot:not(.red)').hide(); /* or .remove() */ 
var $red = $('.left,.right').find('.dot.red').appendTo('.left'); 
var len = Math.round($red.length/2); 
$('.left .dot.red:gt('+(len-1)+')').appendTo('.right'); /* :gt is zero-based */ 

http://jsfiddle.net/mblase75/nnhBp/

http://api.jquery.com/gt-selector

0

你可以使用

$(function(){ 
    var dots = $('.dot'), 
     left = $('.left'), 
     right = $('.right'); 

    $('button').on('click', function(){ 
     var filter = $(this).data('filter'); // here you define which class to remain (on demo i have added a data-filter attribute on the buttons) 
     var filtered = dots.detach().filter('.' + filter), 
      half = Math.ceil(filtered.length/2); 

     filtered.each(function(index){ 
      var target = left; 
      if (index >= half){ 
       target = right; 
      } 
      $(this).appendTo(target); 
     }); 

    }); 
}); 

演示在http://jsfiddle.net/hnaUK/1/

0

我知道你有一些选择,但我从事这个了一下,想我会分享我太:

这是我的jQuery:

$(document).ready(function() { 
     $("button").click(function() { 
      var classVal = "." + $(this).val(); 

      $(".dot").hide(); 
      $(classVal).show(); 

      var halfOf = ($(classVal).length/2)-1; 

      $(classVal).appendTo(".left"); 
      $(classVal+":gt('"+halfOf+"')").appendTo(".right"); 
     });     
    }); 

这里是HTML,都是一样的,但我不得不添加你谈到的按钮:

<div class="left"> 
    <div class="dot blue">blue one</div> 
    <div class="dot red">red one</div> 
    <div class="dot orange">orange one</div> 
    <div class="dot red">red two</div> 
    <div class="dot red">red three</div> 
</div> 
<div class="right"> 
    <div class="dot red">red four</div> 
    <div class="dot blue">blue two</div> 
    <div class="dot blue">blue three</div> 
    <div class="dot blue">blue four</div> 
    <div class="dot orange">orange two</div> 
</div> 
<div id="controls"> 
    <button value="red" type="button">Red</button> 
    <button value="blue" type="button">Blue</button> 
    <button value="orange" type="button">Orange</button> 
</div> 

然后一些简单的CSS:

.left { 
    float: left; 
    border: 1px solid #000000; 
} 
.right { 
    float: left; 
    border: 1px solid #000000; 
} 
#controls { 
    clear: both; 
} 

随着小提琴:

http://jsfiddle.net/CTCPZ/